简体   繁体   中英

pytest run only the changed file?

I'm fairly new to Python, trying to learn the toolsets.

I've figured out how to get py.test -f to watch my tests as I code. One thing I haven't been able to figure out is if there's a way to do a smarter watcher, that works like Ruby's Guard library.

Using guard + minitest the behavior I get is if I save a file like my_class.rb then my_class_test.rb is executed, and if I hit enter in the cli it runs all tests.

With pytest so far I haven't been able to figure out a way to only run the test file corresponding to the last touched file, thus avoiding the wait for the entire test suite to run until I've got the current file passing.

How would you pythonistas go about that?

Thanks!

One possibility is using pytest-testmon together with pytest-watch .

It uses coverage.py to track which test touches which lines of code, and as soon as you change a line of code, it re-runs all tests which execute that line in some way.

To add to @The Compiler 's answer above, you can get pytest-testmon and pytest-watch to play together by using pytest-watch's --runner option:

ptw --runner "pytest --testmon"

Or simply:

ptw -- --testmon

There is also pytest-xdist which has a feature called:

--looponfail: run your tests repeatedly in a subprocess. After each run py.test waits until a file in your project changes and then re-runs the previously failing tests. This is repeated until all tests pass after which again a full run is performed.

If you are using git as version control, you could consider using pytest-picked . This is a plugin that according to the docs:

Run the tests related to the unstaged files or the current branch

Demo

在此处输入图片说明

Basic features

  • Run only tests from modified test files
  • Run tests from modified test files first, followed by all unmodified tests

Usage

pytest --picked

The fastest setup I got was when I combines @lmiguelvargasf @BenR and @TheCompiler answer into this

ptw --runner "pytest --picked --testmon"

you first gotta have them installed by

pip3 install pytest-picked pytest-testmon pytest-watch

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