Package: pytest-watch
Current version on PyPISay Thanks!pytest-watcha zero-config CLI tool that runspytest, 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.MotivationWhether 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 runningpy.testeach 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.Installation$pipinstallpytest-watchUsage$cdmyproject$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 cannotify youwhen tests pass or fail:OSX$ ptw--onpass"say passed"--onfail"say failed"$ptw--onpass"growlnotify -m \"All tests passed!\""\--onfail"growlnotify -m \"Tests failed\""usingGrowlNotify.Windows>ptw --onfail flashusingConsole FlashYou can also run a command before the tests run, e.g. seeding your test
database:$ptw--beforeruninit_db.pyOr after they finish, e.g. deleting a sqlite file. Note that this script
receives the exit code ofpy.testas an argument.$ptw--afterruncleanup_db.pyYou can also use a custom runner script for fullpy.testcontrol:$ptw--runner"python custom_pytest_runner.py"Here’s an minimal runner script that runspy.testand prints its
exit code:# custom_pytest_runner.pyimportsysimportpytestprint('py.test 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:$ ptw --help
Usage: ptw [options] [--ignore <dir>...] [<directory>...] [-- <pytest-args>...]

Options:
  --ignore <dir>        Ignore directory from being watched and during
                        collection (multi-allowed).
  --ext <exts>          Comma-separated list of file extensions that can
                        trigger a new test run when changed (default: .py).
                        Use --ext=* to allow any file (including .pyc).
  --config <file>       Load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  -c --clear            Clear the screen before each run.
  -n --nobeep           Do not beep on failure.
  -w --wait             Waits for all tests to complete before re-running.
                        Otherwise, tests are interrupted on filesystem events.
  --beforerun <cmd>     Run arbitrary command before tests are run.
  --afterrun <cmd>      Run arbitrary command on completion or interruption.
                        The exit code of "py.test" is passed as an argument.
  --onpass <cmd>        Run arbitrary command on pass.
  --onfail <cmd>        Run arbitrary command on failure.
  --onexit <cmd>        Run arbitrary command when exiting pytest-watch.
  --runner <cmd>        Run a custom command instead of "py.test".
  --pdb                 Start the interactive Python debugger on errors.
                        This also enables --wait to prevent pdb interruption.
  --spool <delay>       Re-run after a delay (in milliseconds), allowing for
                        more file system events to queue up (default: 200 ms).
  -p --poll             Use polling instead of OS events (useful in VMs).
  -v --verbose          Increase verbosity of the output.
  -q --quiet            Decrease verbosity of the output (precedence over -v).
  -V --version          Print version and exit.
  -h --help             Print help and exit.ConfigurationCLI options can be added to a[pytest-watch]section in yourpytest.ini fileto
persist them in your project. For example:# pytest.ini[pytest]addopts=--maxfail=2[pytest-watch]ignore=./integration-testsnobeep=TrueAlternativesxdistoffers the--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 py.test, whereas
pytest-watch doesn’t.Noseyis the original codebase
this was forked from. Nosey runsnoseinstead of pytest.ContributingCheck the open issues or open a new issue to start a discussion
around your feature idea or the bug you foundFork the repository, make your changes, and add yourself toAuthors.mdSend a pull requestIf you want to edit the README, be sure to make your changes toREADME.mdand run the following to regenerate theREADME.rstfile:$pandoc-trst-oREADME.rstREADME.mdIf your PR has been waiting a while, feel free toping me on
Twitter.Use this software often? :smiley: