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!

Asked By: Andrew

||

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.

Answered By: The Compiler

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.

Answered By: einSelbst

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
Answered By: Ben R

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

enter image description here

Basic features

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

Usage

pytest --picked
Answered By: lmiguelvargasf

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
Answered By: Norfeldt
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.