[Tutor] Proper way to unit test the raising of exceptions?

Simon Connah sc_opensource at yahoo.com
Sun Apr 1 06:57:15 EDT 2018


Hi,
I'm just wondering what the accepted way to handle unit testing exceptions is? I know you are meant to use assertRaises, but my code seems a little off.
try:    some_func()
except SomeException:    self.assertRaises(SomeException)
Is there a better way to do this at all? The problem with the above code is that if no exception is raised the code completely skips the except block and that would mean that the unit test would pass so, I considered adding:
self.fail('No exception raised')
at the end outside of the except block but not sure if that is what I need to do.
Any help is appreciated.


More information about the Tutor mailing list