Fix Python – Python unittest – opposite of assertRaises?

Question

Asked By – glaucon

I want to write a test to establish that an Exception is not raised in a given circumstance.

It’s straightforward to test if an Exception is raised …

sInvalidPath=AlwaysSuppliesAnInvalidPath()
self.assertRaises(PathIsNotAValidOne, MyObject, sInvalidPath) 

… but how can you do the opposite.

Something like this i what I’m after …

sValidPath=AlwaysSuppliesAValidPath()
self.assertNotRaises(PathIsNotAValidOne, MyObject, sValidPath) 

Now we will see solution for issue: Python unittest – opposite of assertRaises?


Answer

def run_test(self):
    try:
        myFunc()
    except ExceptionType:
        self.fail("myFunc() raised ExceptionType unexpectedly!")

This question is answered By – DGH

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0