Skip to main content

Welcome to pytest-watcher

PyPI PyPI - Python Version GitHub

Overview

pytest-watcher is a tool to automatically rerun tests (using pytest by default) whenever your code changes.

Works on Unix (Linux, MacOS, BSD) and Windows.

Example:

Preview

Table of contents

Motivation

Why not general tools

  • Easy to use and remember
  • Works for most Python projects out of the box
  • Uses native system monitoring API instead of polling on supported systems (see watchdog documentation)
  • Listens for new file, delete file, change and move events
  • Runs your tests with latest changes in case of post-processing events (see delay)
  • Has an interactive mode with handy keyboard shortcuts (Currently only available on POSIX systems)

What about pytest-watch

pytest-watch has been around for a long time and used to address exactly this problem. Unfortunately, pytest-watch is no longer maintained and doesn't work for many users. This project provides an alternative for it.

See also: Differences with pytest-watch

File events

By default pytest-watcher looks for the following events:

  • New *.py file created
  • Existing *.py file modified
  • Existing *.py file deleted
  • A *.py file moved either from or to the watched path

You can specify alternative file patterns to watch. See Watching different patterns

Installation

pip install pytest-watcher

Usage

Specify the path that you want to watch:

ptw .

or

ptw /home/repos/project

pytest-watcher will pass any arguments (excepted reserved options) after <path> to the test runner (which is pytest by default). For example:

ptw . -x --lf --nf

will call pytest with the following arguments:

pytest -x --lf --nf

Available options

The following options are reserved for pytest-watcher and will not be passed to the test runner:

  • --runner - Specify an alternative test runner
  • --patterns - Specify file patterns to watch
  • --ignore-patterns - Specify file patterns to ignore
  • --now - Run tests immediately after starting the watcher
  • --delay - Specify the delay before running tests
  • --clear - Clear the terminal screen before each test run
  • --notify-on-failure - Send BEL notification on test run failure

Debug logging

Set PTW_DEBUG=1 to enable debug-level logging for troubleshooting:

PTW_DEBUG=1 ptw .

Using a different test runner

You can specify an alternative test runner using the --runner flag:

ptw . --runner tox

Watching different patterns

You can use the --patterns flag to specify file patterns that you want to watch. It accepts a list of Unix-style patterns separated by a comma. The default value is "*.py"

Example:

ptw . --patterns '*.py,pyproject.toml'

You can also ignore certain patterns using the --ignore-patterns flag:

ptw . --ignore-patterns 'settings.py,db.py'

Delay

pytest-watcher uses a short delay (0.2 seconds by default) before triggering the actual test run. The main motivation for this is post-processors that can run after you save the file (for example, black plugin in your IDE). This ensures that tests will run with the latest version of your code.

You can control the actual delay value with the --delay flag:

ptw . --delay 0.2

To disable the delay altogether, you can set zero as a value:

ptw . --delay 0

Screen clearing

Use the --clear flag to clear the terminal screen before each test run

ptw . --clear

Enable BEL notification

Use the --notify-on-failure flag to emit the BEL symbol to the terminal when the test run fails. This will trigger an audible or visual alert in some terminal applications.

ptw . --notify-on-failure

Differences with pytest-watch

Even though this project was inspired by pytest-watch, it's not a fork of it. Therefore, there are differences in behavior:

  • pytest-watcher needs you to specify a path to watch as a first argument:
ptw .
  • pytest-watcher doesn't start tests immediately by default. You can customize this behavior using --now flag.

Configuring

You can configure pytest-watcher via pyproject.toml file. Here is the default configuration:

[tool.pytest-watcher]
now = false
clear = true
notify_on_failure = false
delay = 0.2
runner = "pytest"
runner_args = []
patterns = ["*.py"]
ignore_patterns = []

Compatibility

The code is compatible with Python versions 3.9+

Contributing

Please, check CONTRIBUTING.md

License

This project is licensed under the MIT License.

Release files for pytest-watcher 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-watcher 0.6.3
File Size Uploaded
pytest_watcher-0.6.3.tar.gz 601.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-watcher 0.6.3
File Interpreter ABI Platform
pytest_watcher-0.6.3-py3-none-any.whl Python 3 none any Details

Total release size: 614.3 kB

Release files / pytest_watcher-0.6.3.tar.gz

Download URL pytest_watcher-0.6.3.tar.gz
Size 601.9 kB
Tags Source
SHA-256 checksum
How to use checksums
842dc904264df0ad2d5264153a66bb452fccfa46598cd6e0a5ef1d19afed9b13
BLAKE2b-256 checksum
How to use checksums
e6d280606077b7fa8784417687f494ff801d7ab817d9a17fc94305811d5919bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_watcher-0.6.3-py3-none-any.whl

Download URL pytest_watcher-0.6.3-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
83e7748c933087e8276edb6078663e6afa9926434b4fd8b85cf6b32b1d5bec89
BLAKE2b-256 checksum
How to use checksums
fc3f172d73600ad2771774cda108efb813fc724fc345e5240a81a1085f1ade5d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.6.3 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page