Created
October 23, 2018 10:11
-
-
Save hzlmn/6b7bc384301afefcac6de3829bd4c032 to your computer and use it in GitHub Desktop.
Python unittest: Testing an exception is not raised
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from contextlib import contextmanager | |
class TestCase(unittes.TestCase): | |
@contextmanager | |
def assertNotRaises(self, exc_type): | |
try: | |
yield None | |
except exc_type: | |
raise self.failureException('{} raised'.format(exc_type.__name__)) | |
def test_it_does_not_raise_key_error(self): | |
data = {} | |
with self.assertNotRaise(KeyError): | |
data['missing key'] |
And a second typo in the with self ...
statement ... the assertNotRaise misses the trailing s too.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a typo ->
class TestCase(unittes.TestCase):
unittes
should beunittest
.