14

Here is a sample script that checks for a precondition in the very first test case and my intention is to abort the script if the precondition is not met.

#!/usr/bin/python
import unittest
import sys

class TestMyScript(unittest.TestCase):
    def test_000_prerequisite(self):
        a = 0
        if not a:
            sys.exit()
        return

    def test_001_test1(self):
        print "Inside test 1"
        return

    def test_002_test2(self):
        print "Inside test 2"
        return

if __name__ == "__main__":
    unittest.main()

However, the sys.exit() only exits from the individual test case of the suite. It doesn't exit the whole script.

I understand that unittest treats each test case individually which is why any exceptions caused by any testcase are handled by the test runner and it proceeds to the next test case.

But I want the script to kill itself. How do I do that?

Here is the output of my script:

./temp.py
EInside test 1
.Inside test 2
.
======================================================================
ERROR: test_000_prerequisite (__main__.TestMyScript)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./temp.py", line 9, in test_000_prerequisite
    sys.exit()
SystemExit

----------------------------------------------------------------------
Ran 3 tests in 0.000s

FAILED (errors=1)

My guess is that I have to mess around with TestRunner and kill the script if a test case returns some signal. But I am not sure how to really achieve it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gixxer
  • 814
  • 1
  • 10
  • 25
  • 1
    You're catching `SystemExit`. – The name's Bob. MS Bob. Apr 22 '15 at 00:19
  • This might help: http://stackoverflow.com/questions/12901338/python-unittest-cancel-all-tests-if-a-specific-test-fails – Totem Apr 22 '15 at 00:21
  • Duplicate, see link above. – The name's Bob. MS Bob. Apr 22 '15 at 00:29
  • Thank you guys for the response. The above thread lead to another which solved my problem. The easiest way is to add failfast flag. I updated my post with the solution. – gixxer Apr 22 '15 at 00:33
  • 5
    If you have found a solution for your question, I would recommend submitting it as an answer, then accepting the answer. That way others will not continue coming to your question hoping to help when you have already solved the problem. – Vorticity Apr 22 '15 at 01:16
  • possible duplicate of [Stop testsuite if a testcase find an error](http://stackoverflow.com/questions/6813837/stop-testsuite-if-a-testcase-find-an-error) – Joe Apr 23 '15 at 00:29

1 Answers1

6

Here is the answer:

Stop testsuite if a testcase find an error

Here is the change I need to make when calling unittest.main(). The failfast keyword argument stops the script after the first failure.

if __name__ == "__main__":
    unittest.main(failfast=True)

p.s. failfast keyword argument is only available for python 2.7+

p.p.s. you can also use failfast on unittest.TextTestRunner()

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
gixxer
  • 814
  • 1
  • 10
  • 25