Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

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)

1 Answer

0 votes
by (106k points)

If you want to write a test to establish that an Exception is not raised in a given circumstance you can use the following piece of code:-

def run_test(self):

try:

myFunc()

except ExceptionType:

self.fail("myFunc()raised ExceptionType unexpectedly!")

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...