Reputation: 4411
In my test.py I have:
with self.assertRaises(ValidationError):
validate_zipfile(test_zip_path + '.zip')
And this works as intended. I also want to access the error message this ValidationError raises, so I can do this:
self.assertEqual(#error that I extract from the code above, 'Zip file not in correct format.')
Upvotes: 0
Views: 1617
Reputation: 1122142
Store the assertRaises()
context manager, it has an exception
attribute for you to introspect the exception raised:
with self.assertRaises(ValidationError) as exception_cm:
validate_zipfile(test_zip_path + '.zip')
exception = exception_cm.exception
self.assertIn('Zip file not in correct format.', exception.messages)
You could use the Django-specific assertRaisesMessage()
method, but take into account that that test does a simple substring test (e.g. you could potentially run into a false positive where you test for a substring of a longer message). Since ValidationError
handles a list of messages, a test against ValidationError.messages
is going to be more robust.
Upvotes: 6