Skip to content

Added unittest self.assertRaises() in context manager. #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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')