Pytest Testmon
pytest-testmon is a pytest plugin that intelligently selects and executes only tests affected by recent code changes. It operates by collecting dependencies between tests and the executed code (using Coverage.py) and storing this information in a local database (.testmondata). This database is updated on each test run, allowing the plugin to work independently of version control systems. The latest version is 2.2.0, released in late 2025.
Warnings
- breaking After upgrading pytest-testmon to a new version, the `.testmondata` file might be in an incompatible format. It's crucial to remove this file to avoid issues and allow a new database to be built.
- gotcha When combining `pytest-testmon` with `pytest-watch` for continuous testing, `testmon` can become out of sync if interrupted, leading to incomplete results. This is because `testmon` is stateful.
- gotcha pytest-testmon may re-execute more tests than anticipated, especially if changes are made to fundamental parts of the codebase like method parameter names. This is often by design, as such changes can have broad impacts.
- gotcha For `pytest-testmon` to effectively select a subset of tests, an initial full run of your test suite with the `--testmon` option is required to build its dependency database.
- deprecated The `run_variants` configuration option was removed in version 0.4+. It has been replaced by `run_variant_expression` for separating coverage data for different environments.
Install
-
pip install pytest-testmon
Quickstart
# 1. Install pytest-testmon # pip install pytest-testmon # 2. Run all tests once to build the initial dependency database # This creates a .testmondata file # pytest --testmon # 3. Make some code changes that affect a subset of your tests # 4. Run pytest-testmon again to execute only affected tests pytest --testmon # Example for CI/CD, if you want separate coverage data # import os # os.environ['TESTMON_DATAFILE'] = 'testmondata_ci' # pytest --testmon