Local continuous test runner with pytest and watchdog.
Local continuous test runner with pytest and watchdog.
pytest-watch a zero-config CLI tool that runs pytest, and re-runs it when a file in your project changes. It beeps on failures and can run arbitrary commands on each passing and failing test run.
Whether or not you use the test-driven development method, running tests
continuously is far more productive than waiting until you're finished
programming to test your code. Additionally, manually running pytest each
time you want to see if any tests were broken has more wait-time and cognitive
overhead than merely listening for a notification. This could be a crucial
difference when debugging a complex problem or on a tight deadline.
$ pip install pytest-watch$ cd myproject
$ ptw
* Watching /path/to/myprojectNote: It can also be run using its full name pytest-watch.
Now develop normally and check the terminal every now and then to see if any tests are broken. Alternatively, pytest-watch can notify you when tests pass or fail:
OSX
$ ptw --onpass "say passed" --onfail "say failed"
$ ptw --onpass "growlnotify -m \"All tests passed!\"" \
--onfail "growlnotify -m \"Tests failed\""using GrowlNotify.
Windows
> ptw --onfail flashusing Console Flash
You can also run a command before the tests run, e.g. seeding your test database:
$ ptw --beforerun init_db.pyOr after they finish, e.g. deleting a sqlite file. Note that this script receives
the exit code of pytest as an argument.
$ ptw --afterrun cleanup_db.pyYou can also use a custom runner script for full pytest control:
$ ptw --runner "python custom_pytest_runner.py"Here's an minimal runner script that runs pytest and prints its exit code:
# custom_pytest_runner.py
import sys
import pytest
print('pytest exited with code:', pytest.main(sys.argv[1:]))Need to exclude directories from being observed or collected for tests?
$ ptw --ignore ./deep-directory --ignore ./integration_testsSee the full list of options:
…CLI options can be added to a [pytest-watch] section in your
pytest.ini file to persist them in your project. For example:
# pytest.ini
[pytest]
addopts = --maxfail=2
[pytest-watch]
ignore = ./integration-tests
nobeep = True--looponfail (-f) option (and distributed testing
options). This instead re-runs only those tests which have failed until you
make them pass. This can be a speed advantage when trying to get all tests
passing, but leaves out the discovery of new failures until then. It also
drops the colors outputted by pytest, whereas pytest-watch doesn't.If you want to edit the README, be sure to make your changes to README.md and
run the following to regenerate the README.rst file:
$ pandoc -t rst -o README.rst README.mdIf your PR has been waiting a while, feel free to ping me on Twitter.
Use this software often? :smiley:
No open issues yet, or sync has not completed.