HS.Hubert Swic
Back to projects

Developer Tool / Completed

Pytest File Watcher

Tool that watches your files and runs your pytest tests automatically when it detects changes.

PythonGithubShell

Mar 2023 — Mar 2023

Overview

Pytest File Watcher is a lightweight Python CLI tool designed to automatically rerun a project's test suite whenever source files change.

The project started from a simple frustration: repeatedly switching back to a terminal and manually running pytest during development. Existing tools such as pytest-watch already addressed the problem but haven't been updated in years. I wanted something smaller, more opinionated, and better suited to the way I worked across multiple projects.

The result was a command-line utility that could remain running in the background, detect changes to a Python project, and immediately execute its tests.

More importantly, I wanted the configuration to live with the project rather than in my memory. Instead of remembering long combinations of pytest flags every time I returned to a codebase, those options could be stored once and reused.

The project was eventually packaged and published to PyPI as pytest-file-watcher, with the pytest-w command acting as its CLI entrypoint.

The problem

Testing frequently during development is valuable, but the feedback loop becomes unnecessarily repetitive when every change requires manually returning to a terminal and rerunning the same command.

That becomes even more noticeable when working across multiple projects.

A project might require verbose output, custom pytest arguments, ignored directories, additional watched file extensions, or a particular failure configuration. The underlying command may not be complicated, but repeatedly remembering and reconstructing it creates friction.

There were already tools designed to automatically rerun pytest, particularly pytest-watch, but at the time I wanted something deliberately simpler and more closely aligned with my own workflow.

The central problem therefore was not simply:

"How can I run pytest when a file changes?"

It was:

"How can I make continuous testing effectively disappear into the development workflow?"

The tool needed to watch the project, understand project-specific configuration, trigger tests automatically, and provide enough extensibility that the developer could react to successful or failed test runs without constantly monitoring the terminal.

The solution

Pytest File Watcher wraps file-system monitoring and pytest execution behind a small CLI built with Typer.

Running the test command starts a watcher against a selected project path. The application uses watchfiles to monitor changes and automatically launches pytest whenever relevant files are modified.

Rather than forcing every option into the command line, the tool supports a reusable YAML configuration file containing project-specific behaviour.

Configuration can control options such as:

- verbose pytest output

- automatically clearing the terminal between runs

- ignored files and directories

- additional watched extensions

- pytest passthrough arguments

- commands to execute after successful tests

- commands to execute after failed tests

CLI arguments can also be supplied when required, allowing temporary command-line options to override or complement persistent project configuration.

One particularly useful addition was the ability to execute arbitrary shell commands based on the result of the test run.

For example, while working in WSL I could use Windows notifications to report whether the latest test run succeeded or failed. This meant the watcher could remain running in another terminal while I continued working, turning tests into an asynchronous feedback mechanism rather than something I had to continually check.

The tool was then packaged as a standard Python distribution and published to PyPI, making installation as simple as pip install pytest-file-watcher.

Architecture & technical approach

The project uses a deliberately small architecture built around three responsibilities: configuration, file watching, and test execution.

The CLI layer is implemented with Typer and exposes the pytest-w command. It handles commands and options before translating them into a configuration object that is passed into the watcher.

The watcher layer uses watchfiles, specifically its process-running functionality and Python-aware filtering. A configured project path is monitored while ignored paths and additional extensions are passed into the watch filter.

When a change is detected, the watcher triggers the test runner. The runner constructs the appropriate pytest command, adds configured passthrough arguments, and executes it as a subprocess.

The subprocess exit code then becomes part of the application's workflow:

- an exit code of 0 represents a successful test run

- a non-zero result represents failure

- configured onPass or onFail commands can then be executed

This allows test execution to remain decoupled from whatever action the developer wants to perform afterwards, whether that is a desktop notification, script, sound, or another local automation.

A YAML configuration layer sits above this runtime behaviour, allowing projects to persist their preferred test workflow without embedding those decisions directly into the code or requiring long shell commands.

The final application remains intentionally compact: a CLI orchestrates configuration, the watcher responds to filesystem events, pytest handles the tests themselves, and subprocess hooks provide a simple extension mechanism.

Key features

  • Automatic pytest execution when project files change
  • Simple pytest-w command-line interface
  • Project-level YAML configuration
  • CLI options for temporary configuration overrides
  • Automatic terminal clearing between test runs
  • Configurable ignored files and directories
  • Support for additional watched file extensions
  • Native passthrough of arguments to pytest
  • Commands triggered after successful test runs
  • Commands triggered after failed test runs
  • Desktop notification integration through external commands
  • Python-aware file watching
  • Background continuous-testing workflow
  • Published Python package installable directly from PyPI
  • Built-in CLI help and version information

Technical highlights

  • Written entirely in Python
  • Typer used to create a structured CLI and subcommand interface
  • watchfiles used for efficient filesystem event monitoring
  • PythonFilter used to customise ignored paths and watched extensions
  • PyYAML used for persistent project configuration
  • Rich used for improved terminal output
  • pytest executed through subprocesses rather than tightly coupling the watcher to pytest internals
  • Process exit codes used to drive success and failure hooks
  • Arbitrary shell hooks provide a lightweight extension mechanism
  • Clear separation between CLI configuration, file monitoring and test execution
  • Packaged with setuptools
  • Exposes a dedicated pytest-w console entrypoint
  • Supports Python 3.9+
  • Released publicly under the MIT licence
  • Published to PyPI as a reusable developer tool
  • Designed around shortening the local development feedback loop
Discuss a similar project