Skip to content Skip to sidebar Skip to footer

Unit Test Problem With Assertraises

I am trying to test for an exception. I have: def test_set_catch_status_exception(self): mro = self.mro NEW_STATUS = 'No such status' self.assertRaises(ValueError,mro.s

Solution 1:

self.assertRaises expects a function mro.setStatus, followed by an arbitrary number of arguments: in this case, just NEW_STATUS. self.assertRaises assembles its arguments into the function call mro.setStatus(NEW_STATUS) inside a try...except block, thus catching and recording the ValueError if it occurs.

Passing mro.setStatus(NEW_STATUS) as an argument to self.assertRaises causes the ValueError to occur before self.assertRaises can trap it.

So the fix is to change the parentheses to a comma:

self.assertRaises(ValueError,mro.setStatus,NEW_STATUS)

Solution 2:

Be careful if you're using factory boy, this package doesn't allow the exception to be raised to the assert level which will always fail

Post a Comment for "Unit Test Problem With Assertraises"