{"id":3246,"library":"pytest-testmon","title":"Pytest Testmon","description":"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.","status":"active","version":"2.2.0","language":"en","source_language":"en","source_url":"https://github.com/tarpas/pytest-testmon","tags":["pytest-plugin","testing","developer-tooling","test-optimization","ci/cd"],"install":[{"cmd":"pip install pytest-testmon","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"pytest-testmon is a plugin for pytest.","package":"pytest","optional":false},{"reason":"Used internally by pytest-testmon to collect code execution data.","package":"coverage","optional":false},{"reason":"Often used in conjunction for continuous test execution, though specific usage caveats apply.","package":"pytest-watch","optional":true}],"imports":[],"quickstart":{"code":"# 1. Install pytest-testmon\n# pip install pytest-testmon\n\n# 2. Run all tests once to build the initial dependency database\n#    This creates a .testmondata file\n# pytest --testmon\n\n# 3. Make some code changes that affect a subset of your tests\n\n# 4. Run pytest-testmon again to execute only affected tests\npytest --testmon\n\n# Example for CI/CD, if you want separate coverage data\n# import os\n# os.environ['TESTMON_DATAFILE'] = 'testmondata_ci'\n# pytest --testmon","lang":"bash","description":"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."},"warnings":[{"fix":"Delete the `.testmondata` file from your project's root directory after each upgrade: `rm .testmondata`.","message":"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.","severity":"breaking","affected_versions":"<=2.1.x to 2.2.0 (and potentially future major/minor upgrades)"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always perform an initial `pytest --testmon` run to populate the `.testmondata` file before expecting `testmon` to intelligently select tests.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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])`.","message":"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.","severity":"deprecated","affected_versions":"<0.4"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}