Skip to main content

Run only tests impacted by your code changes (delta-based selection) for pytest.

Project description

pytest-delta

A pytest plugin that runs only the tests affected by your code changes. Instead of running your entire test suite every time, pytest-delta uses content hashing and AST-based dependency analysis to figure out which tests actually need to run.

How It Works

  1. On the first run, all tests execute normally. pytest-delta builds a dependency graph by parsing imports across your Python files and saves a snapshot (file hashes + graph) to a .delta.msgpack file.

  2. On subsequent runs, it compares current file hashes against the snapshot to detect changes. Using the reverse dependency graph, it identifies all files transitively affected by those changes and runs only the corresponding tests.

  3. If a conftest.py changes, all tests in its directory and subdirectories are re-run (conservative approach).

  4. The delta file is only updated when all tests pass. If tests fail, the previous snapshot is preserved so those tests run again next time.

Installation

pip install pytest-delta

Or with Poetry:

poetry add pytest-delta

Usage

Enable the plugin with the --delta flag:

# First run: executes all tests, creates .delta.msgpack
pytest --delta

# Second run: no changes detected, exits 0 immediately
pytest --delta

# After modifying src/utils.py: only affected tests run
pytest --delta

Use --delta-debug to see what the plugin is doing:

pytest --delta --delta-debug
[pytest-delta] Plugin enabled
[pytest-delta] Loaded delta: 42 files tracked
[pytest-delta] Changed: 1, New: 0, Deleted: 0
[pytest-delta] Affected test files: 3
[pytest-delta]   tests/test_api.py
[pytest-delta]   tests/test_models.py
[pytest-delta]   tests/test_utils.py
[pytest-delta] Selected: 12, Deselected: 85

CLI Options

Option Description
--delta Enable delta-based test filtering
--delta-file PATH Custom delta file path (default: .delta.msgpack)
--delta-rebuild Force rebuild the dependency graph from scratch
--delta-no-save Don't save the delta file after the run (read-only mode)
--delta-debug Print debug information about filtering decisions

Markers

Force specific tests to always run regardless of changes:

import pytest

@pytest.mark.delta_always
def test_smoke():
    """This test runs every time, even if nothing changed."""
    assert app.health_check() == "ok"

CI Workflow

A typical workflow:

  1. Developer runs pytest --delta locally. Tests pass, .delta.msgpack is updated.
  2. Developer commits the delta file along with their changes.
  3. CI runs pytest --delta. Since the delta file reflects the already-validated state, no tests need to run and CI exits 0 immediately.

For CI pipelines where you want to prevent accidental delta file updates:

pytest --delta --delta-no-save

Change Detection

pytest-delta uses content hashing (SHA-256) to detect changes. This means:

  • No dependency on git history or commit hashes
  • Works with uncommitted and staged changes
  • No issues with rebasing or squash merges
  • Detects changes regardless of how they were made

Dependency Analysis

The plugin builds a dependency graph by parsing Python AST import statements:

  • Handles absolute imports (import pkg.mod, from pkg.mod import func)
  • Handles relative imports (from .utils import helper, from ..core import Base)
  • Tracks __init__.py as implicit dependencies of package modules
  • Computes transitive closure: if A imports B and B imports C, changing C re-runs tests for both A and B

Limitations:

  • Only tracks .py files (non-Python config/data files are ignored)
  • Dynamic imports (importlib.import_module()) are not detected
  • Only project-local imports are tracked (third-party packages are ignored)

Configuration

Add .delta.msgpack to your .gitignore if you don't want to share the delta file across developers, or commit it if you want the CI optimization described above.

# Uncomment to exclude from version control
# .delta.msgpack

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_delta-2.0.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

pytest_delta-2.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest_delta-2.0.0.tar.gz.

File metadata

  • Download URL: pytest_delta-2.0.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.9.18 Darwin/25.2.0

File hashes

Hashes for pytest_delta-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4a922fe607ee52114b158bfee8bba83ad84c27cfed7267c041d9e087f4da1a88
MD5 45ebfe30d5151d788a5ea69b3a0cee17
BLAKE2b-256 1bea29b6e5e5d777fd449aedcf7377e4ab42e32d226b4403c6c2e8cafd632de2

See more details on using hashes here.

File details

Details for the file pytest_delta-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_delta-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.9.18 Darwin/25.2.0

File hashes

Hashes for pytest_delta-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11e6aee760d6cac7e94571475e2d0c542a3568b0a36c7cf3781e743a2bf64b2f
MD5 fdc6bdaad51cabb7f5b28495d2374832
BLAKE2b-256 b8ee224bd3d88f724b799100cad6bcb096b6af53cf64497bf1aaf568256e7f12

See more details on using hashes here.

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