Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop testsuite if a testcase find an error

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.

like image 310
Gabriel Quesada Avatar asked Jul 25 '11 09:07

Gabriel Quesada


People also ask

How do you stop a test in Python?

Once you are in a TestCase , the stop() method for the TestResult is not used when iterating through the tests. Somewhat related to your question, if you are using python 2.7, you can use the -f/--failfast flag when calling your test with python -m unittest . This will stop the test at the first failure. Thanks.

How do you skip a test case in python?

Alternate syntax for skipping test is using instance method skipTest() inside the test function.

What is Unittest TestCase in Python?

A test case is the individual unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase , which may be used to create new test cases.


2 Answers

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

Example:

if __name__ == '__main__':
    unittest.main(failfast=True)
like image 90
Jet_C Avatar answered Oct 04 '22 19:10

Jet_C


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)
like image 44
raspberrybi Avatar answered Oct 04 '22 19:10

raspberrybi