Sanket
Sanket

Reputation: 746

Force a unit test case to ERROR in Python

I went through the Unit Test Case module of python and found that there is way to force a test case to fail using the TestCase.fail() object. However I did not find anything that would force a test case to error. Any ideas on how this could be done?

EDIT:

More explaination

There are few test scripts which were written long ago and which I'm using right now for testing a firmware. These test scripts use the UnitTest module of python. The test scripts also import a bunch of other user-written modules. All these user written modules throw few exceptions. So whenever a test case doesn't call methods from user-written modules with correct inputs exception are thrown. the UnitTest module rightly flags all such test cases as errors. But by looking at the output of UnitTest module, which is just a Traceback to the line where exception was generated, it is not immediately clear to me because of which input the exception was generated. So I used try-except construct in the test scripts to catch the exceptions and print out the input which was the reason for exception. But since I handled the exceptions UnitTest was flagging these test cases as pass. Using the raise statement in the except block, as pointed out by @alecxe, solved this issue.

Upvotes: 2

Views: 3770

Answers (1)

alecxe
alecxe

Reputation: 474191

Just use raise to raise an exception:

raise Exception('Manually raised exception')

Upvotes: 6

Related Questions