Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion source/unittest/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ exception in the test. Compare these two tests:
:start-after: #end_pymotw_header

The results for both are the same, but the second test using
``assertRaises()`` is more succinct.
``assertRaises()`` is more succinct. ``assertRaises()`` can also be
run as a context manager.

.. {{{cog
.. cog.out(run_script(cog.inFile, '-m unittest -v unittest_exception.py'))
Expand All @@ -522,6 +523,7 @@ The results for both are the same, but the second test using
$ python3 -m unittest -v unittest_exception.py

testAssertRaises (unittest_exception.ExceptionTest) ... ok
testAssertRaisesContext (unittest_exception.ExceptionTest) ... ok
testTrapLocally (unittest_exception.ExceptionTest) ... ok

----------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions source/unittest/unittest_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ def testAssertRaises(self):
'a',
b='c',
)

def testAssertRaisesContext(self):
with self.assertRaises(ValueError):
raises_error('a', b='c')