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.
pip install pytest-testmonNo compatibility data collected yet for this library.
To begin, install the plugin. The first run with `pytest --testmon` builds a `.testmondata` file containing the dependency database. Subsequent runs with `pytest --testmon` will then identify and execute only the tests impacted by recent code changes. For continuous integration, you might want to manage the `.testmondata` file or use `TESTMON_DATAFILE` environment variable.
Delete the `.testmondata` file from your project's root directory after each upgrade: `rm .testmondata`.
When using with `pytest-watch`, consider running `pytest --testmon --testmon-readonly`. This uses a frozen snapshot of the dependency data and prevents `testmon` from updating its database during `pytest-watch` runs, ensuring consistent test selection.
Understand that `testmon` identifies dependencies at a fine-grained code block level. A seemingly small change to a widely used function or module might correctly trigger many dependent tests. If tests are failing unexpectedly, it might indicate underlying undesired test dependencies in your test suite, which `testmon` helps expose.
Always perform an initial `pytest --testmon` run to populate the `.testmondata` file before expecting `testmon` to intelligently select tests.
Update your configuration (e.g., in `pytest.ini`) to use `run_variant_expression` instead of `run_variants`. For example: `run_variant_expression = os.environ.get('DJANGO_SETTINGS_MODULE') + ':python' + str(sys.version_info[:2])`.