Stop testsuite if a testcase find an error

时光怂恿深爱的人放手 提交于 2019-12-01 01:03:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!