问题
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.
回答1:
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
.
回答2:
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.
回答3:
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.
回答4:
Use failfast=True
it will stop running all tests if 1 fails in your test class
Example:
if __name__ == '__main__':
unittest.main(failfast=True)
回答5:
You can use sys.exit() to close the python interpreter at any point within your testcase.
来源:https://stackoverflow.com/questions/6813837/stop-testsuite-if-a-testcase-find-an-error