Stop testsuite if a testcase find an error

邮差的信 提交于 2019-12-01 03:51:08

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.

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.

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