开发者

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.


Since Python 2.7, unittest support failfast option. It can be either specified by commandline:

python -m unittest -f test_module

Or when using a script:

>>> from unittest import main
>>> main(module='test_module', failfast=True)

Unfortunately I haven't yet found out how to specify this option when you are using setuptools and setup.py.


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)


Are you actually doing unit tests? Or system tests of something else? If the latter, you may be interested in my Python based testing framework. One of its features is exactly this. You can define test case dependencies and the suite will skip tests with failed dependencies. It also has built-in support to selenium and webdriver. However, it's not so easy to set up. Currently in development but mostly works. Runs on Linux.


Run your tests with nose and use the -x flag. That combined with the --failed flag should give you all you need. So in the top level of your project run

nosetests -x # with -v for verbose and -s to no capture stdout

Alternatively you could run with

nosetests --failed

Which will re-run only your failing tests from the test suite

Other useful flags:

nosetests --pdb-failure --pdb

drops you into a debugger at the point your test failed or errorred

nosetests --with-coverage --cover-package=<your package name> --cover-html

gives you a colorized html page showing which lines on your code have been touched by the test run

A combination of all of those usually gives me what I want.


You can use sys.exit() to close the python interpreter at any point within your testcase.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜