diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-02-05 21:07:38 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2010-02-05 21:07:38 (GMT) |
commit | 1f3fa8ae68561c961189f23e96bd4413da434ad1 (patch) | |
tree | f5bb0ee1296656a5b73a69813c5a2620c0a6c61d | |
parent | 1c43001cf3ca0a54e302b0bd8f228aa92dba8776 (diff) | |
download | cpython-1f3fa8ae68561c961189f23e96bd4413da434ad1.zip cpython-1f3fa8ae68561c961189f23e96bd4413da434ad1.tar.gz cpython-1f3fa8ae68561c961189f23e96bd4413da434ad1.tar.bz2 |
Example of using assertRaises as a context manager in the unittest documentation.
-rw-r--r-- | Doc/library/unittest.rst | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index d3b0767..dd0466b 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -895,12 +895,18 @@ Test cases If *callable* is omitted or None, returns a context manager so that the code under test can be written inline rather than as a function:: - with self.failUnlessRaises(some_error_class): + with self.assertRaises(SomeException): do_something() The context manager will store the caught exception object in its :attr:`exc_value` attribute. This can be useful if the intention - is to perform additional checks on the exception raised. + is to perform additional checks on the exception raised:: + + with self.assertRaises(SomeException) as cm: + do_something() + + the_exception = cm.exc_value + self.assertEquals(the_exception.error_code, 3) .. versionchanged:: 2.7 Added the ability to use :meth:`assertRaises` as a context manager. |