简体   繁体   中英

Python unittest, running only the tests that failed

I have a large python test file using unittest that I run from the command line. Some tests take a while to run. This is a mild pain point because I'm often only concerned with the last test I added. What I want is this:

  1. add test.
  2. run tests (one fails because I haven't written the code to make it pass)
  3. implement the behaviour
  4. run only the test that failed last time
  5. fix the silly error I made when implementing the code
  6. run only the failing test, which passes this time
  7. run all the tests to find out what I broke.

Is it possible to do this from the command line?

Every test function is declared like:

    def test_something_something(self):

If you add an underscore in front, like:

    def _test_something_something(self):

that test will be ignored. One thing you can do is to do a quick find and replace in vim . Find all "test_"s and replace them with "_test_" and then find the one test that failed and remove the underscore.

(Not a fully automated solution, but better than the existing one)

If you pass the name of a test class as an argument to the test script, only that test will be run. For example, if you only want to run tests in the MyTest class in the script test_whatever.py :

python3 test_whatever.py MyTest

You can also specify an individual test as a member of that class. For example, suppose you want to run the test test_something in the class MyTest :

python3 test_whatever.py MyTest.test_something

只需使用--last-failed选项运行测试(您可能需要pytest)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM