Watch Source and Re-run Pytest on Change


This is how you can watch for changes to your source file and re-run the test on change with pytest.

Pytest needs a plugin to be able to watch for file changes. Install the simplest pytest setup, including installing this plugin, pytest-watch.

Then you can use the ptw command in place of pytest.

Use this command and pass the path of both the source and the test files. Thus, changes to either will be watched. Imagine a file structure like:

├── __init__.py
├── module.py
├── module_test.py

Match the test and source pattern at once with a substring:

ptw module*

Now changes to either file will re-run the test.