1. Armin Rigo
  2. cpython-withatomic


Michael Foord  committed 27416ce

Example of using assertRaises as a context manager in the unittest documentation.

  • Participants
  • Parent commits 32a360b
  • Branches legacy-trunk

Comments (0)

Files changed (1)

File Doc/library/unittest.rst

View file
       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):
       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.