Stop testsuite if a testcase find an error

Viewed 7999

I have a testSuite in Python with several testCases.

If a testCase fails, testSuite continues with the next testCase. I would like to be able to stop testSuite when a testCase fails or be able to decide if the testSuite should continue or stop.

6 Answers

None of the answers seem to touch on this part of your question:

... be able to decide if the testSuite should continue or stop.

You can override the run() method within your TestCase class and call the TestResult.stop() method to signal to the TestSuite to stop running tests.

class MyTestCase(unittest.TestCase):
    def run(self, result=None):
        if stop_test_condition():
            result.stop()
            return
        super().run(result=result)

Use failfast=True it will stop running all tests if 1 fails in your test class

Example:

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