2

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.

3
  • Could you describe the use case, why do you need this? Commented Aug 7, 2014 at 1:22
  • 1
    The only reason I can imagine needing this is if you are testing a testing framework... If you can explain what you're after, we might have a better idea for you. Commented Aug 7, 2014 at 1:36
  • Hey I provided some more explanation in the question. Hope it is clear. Thanks for your concern Commented Aug 7, 2014 at 16:22

1 Answer 1

6

Just use raise to raise an exception:

raise Exception('Manually raised exception')
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @alecxe.. Exactly what I wanted.
@Sanket just for our own curiosity: what are you using it for?
I think I understood Ned's curiosity. If you're trying to make your test fail, perhaps there is an error on approach to test it.
@MauroBaraldi I do not want to fail the test case. But flag it as an error. I provided some more explanation in the question. Yes I agree the approach is wrong though
@Sanket it looks like you can just use a plain raise which would reraise the exception caught in except block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.