Skip to main content

A pytest plugin that selectively runs tests impacted by code changes via git introspection, AST parsing, and dependency graph analysis.

Project description

pytest-impacted

CI GitHub License PyPI - Version PyPI - Python Version

Run only the tests that matter. A pytest plugin that uses git diff, AST parsing, and dependency graph analysis to selectively run tests impacted by your code changes.

pytest --impacted --impacted-module=my_package     # unstaged changes
pytest --impacted --impacted-module=my_package \
       --impacted-git-mode=branch \
       --impacted-base-branch=main                 # branch changes vs main

Key Features

Feature Details
:zap: Fast feedback Only runs tests affected by your changes — skip the rest
:deciduous_tree: Dependency-aware Follows import chains transitively, not just direct file changes
:gear: No imports at analysis time Filesystem discovery + AST parsing — no module-level side effects
:test_tube: pytest-native Works as a standard pytest plugin with familiar CLI options
:wrench: conftest.py aware Changes to conftest.py automatically impact all tests in scope
:building_construction: CI-friendly Standalone impacted-tests CLI for two-stage CI pipelines
:shield: Helpful errors Validates config early with clear messages and suggestions

[!CAUTION] This project is currently in beta. Please report bugs via the Issues tab.


Installation

pip install pytest-impacted

Or with uv:

uv add pytest-impacted

Requires Python 3.11+.


Quick Start

1. Run tests impacted by uncommitted changes:

pytest --impacted --impacted-module=my_package

2. Run tests impacted by branch changes (vs main):

pytest --impacted \
       --impacted-module=my_package \
       --impacted-git-mode=branch \
       --impacted-base-branch=main

3. Include tests outside the package directory:

pytest --impacted \
       --impacted-module=my_package \
       --impacted-tests-dir=tests

That's it. Unaffected tests are automatically skipped.


How It Works

Git diff → Changed files → Module resolution → AST import parsing → Dependency graph → Impacted tests
  1. Git introspection identifies which files changed (unstaged edits or branch diff)
  2. Filesystem discovery maps file paths to Python module names — without importing anything
  3. AST parsing (via astroid) extracts import relationships from source files
  4. Dependency graph (via NetworkX) traces transitive dependencies from changed modules to test modules
  5. Test filtering skips tests whose modules are not in the impact set

The philosophy is to err on the side of caution: we favor false positives (running a test that didn't need to run) over false negatives (missing a test that should have run).


Usage

Git Modes

Mode Flag What it compares
unstaged (default) --impacted-git-mode=unstaged Working directory changes + untracked files
branch --impacted-git-mode=branch All commits on current branch vs base branch

The --impacted-base-branch flag accepts any valid git ref, including expressions like HEAD~4.

External Tests Directory

When your tests live outside the namespace package (a common layout), use --impacted-tests-dir so the dependency graph includes them:

pytest --impacted \
       --impacted-module=my_package \
       --impacted-tests-dir=tests

CI Integration

For CI pipelines where git access and test execution happen in separate stages, use the impacted-tests CLI to generate the test file list:

# Stage 1: identify impacted tests
impacted-tests --module=my_package --git-mode=branch --base-branch=main > impacted_tests.txt

# Stage 2: run only those tests
pytest $(cat impacted_tests.txt)

Configuration via pyproject.toml

All CLI options can be set as defaults in your pyproject.toml (or pytest.ini):

[tool.pytest.ini_options]
impacted = true
impacted_module = "my_package"
impacted_git_mode = "branch"
impacted_base_branch = "main"
impacted_tests_dir = "tests"

CLI flags override these defaults.

All Options

Option Default Description
--impacted false Enable the plugin
--impacted-module (required) Top-level Python package to analyze
--impacted-git-mode unstaged Git comparison mode: unstaged or branch
--impacted-base-branch (required for branch mode) Base branch/ref for branch-mode comparison
--impacted-tests-dir None Directory containing tests outside the package

Alternatives

Project Notes
pytest-testmon Most popular option. Uses coverage-based granular change tracking. More precise but heavier; may conflict with other plugins.
pytest-picked Runs tests from directly modified files only — no transitive dependency analysis.
pytest-affected Appears unmaintained, no source repository.

Development

This project uses uv for dependency management.

# Setup
uv sync --all-extras --dev

# Run tests
uv run python -m pytest

# Run tests with coverage
uv run python -m pytest --cov=pytest_impacted --cov-branch tests

# Lint + format + type check
pre-commit run --all-files

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_impacted-0.16.0.tar.gz (656.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_impacted-0.16.0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_impacted-0.16.0.tar.gz.

File metadata

  • Download URL: pytest_impacted-0.16.0.tar.gz
  • Upload date:
  • Size: 656.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_impacted-0.16.0.tar.gz
Algorithm Hash digest
SHA256 f8b7834d5828d9a0206de3fa7d7d8b20540a639c1d52ccaad0716537e378ab4e
MD5 29f5d23162683be2ee9126eb70de643a
BLAKE2b-256 1707880c95de7b6665aa167a83f43198a733f8585d37e6f64c9ac0787eb280ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_impacted-0.16.0.tar.gz:

Publisher: publish-to-pypi.yml on promptromp/pytest-impacted

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_impacted-0.16.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_impacted-0.16.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b320617caf8b74d34d689aed1e71ac2aef60717d8865b745525eb51c3dc18ff
MD5 3567268e458ab23b620b2c88f5d3d7f9
BLAKE2b-256 9d49160d60e0a32183171d3a09551c551a70010ad0f4d6791779c9dd3328e1b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_impacted-0.16.0-py3-none-any.whl:

Publisher: publish-to-pypi.yml on promptromp/pytest-impacted

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page