Reputation: 16349
How to write a unit test that is considered failed if an exception is risen?
I've found the assertRaises
here but it seems to be doing the exact opposite.
Example from Django
If something is wrong with the database the entry.save()
will rises an exception.
class TestModel(TestCase):
def test_model_creation(self):
try:
entry = MyModel(name='Bob')
entry.save()
except Exception:
self.assertEqual(0, 1)
The self.assertEqual(0, 1)
is just a bad hack for the test to fail if there is an exception. What would be the right way of doing it?
Upvotes: 1
Views: 57
Reputation: 6053
No need to try and except. If a test raises an error then it fails anyway.
Otherwise, see this post about the same thing.
Upvotes: 2