Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest run only the changed file?

Tags:

python

pytest

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!

like image 760
Andrew Avatar asked Jan 30 '16 03:01

Andrew


People also ask

How do I run a pytest on a specific file?

We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How does pytest know what to run?

Specifying which tests to run Pytest supports several ways to run and select tests from the command-line. This will run tests which contain names that match the given string expression (case-insensitive), which can include Python operators that use filenames, class names and function names as variables.

How do you run failed test cases in pytest?

The plugin provides two command line options to rerun failures from the last pytest invocation: --lf , --last-failed - to only re-run the failures. --ff , --failed-first - to run the failures first and then the rest of the tests.

How do I stop pytest execution?

pytest has the option -x or --exitfirst which stops the execution of the tests instanly on first error or failed test. pytest also has the option --maxfail=num in which num indicates the number of errors or failures required to stop the execution of the tests.


1 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.

like image 197
The Compiler Avatar answered Sep 28 '22 16:09

The Compiler