Skip to content

Instantly share code, notes, and snippets.

@hzlmn
Created October 23, 2018 10:11
Show Gist options
  • Save hzlmn/6b7bc384301afefcac6de3829bd4c032 to your computer and use it in GitHub Desktop.
Save hzlmn/6b7bc384301afefcac6de3829bd4c032 to your computer and use it in GitHub Desktop.
Python unittest: Testing an exception is not raised
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']
@benoit-dubreuil
Copy link

There's a typo -> class TestCase(unittes.TestCase):

unittes should be unittest.

@andreamoro
Copy link

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