pytest run only the changed file?
Question:
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!
Answers:
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.
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.
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
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
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.
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.
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
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