Skip to main content

testscope

Run only the tests your change can affect. Built for AI coding agents and CI.

Coding agents run pytest after every edit. On a 6,000-test suite that is minutes per run. It goes past Claude Code's ~2-minute foreground limit, so the run moves to the background and the agent stops reading its output. testscope records which code and files each test really touches. After that, plain pytest runs only the tests your change can reach.

testscope picks 71 of 6,162 tests for a one-file change

On a real 6,195-test project, over its last 30 commits:

Median tests run per change: full suite 6,195, testmon 584, testscope 353

testscope pytest-testmon import graph
Median tests run per real commit (of 6,195) 353 584 352 files of 378
Injected faults caught (34 that break a test) 33 22 22
Faults in data files caught (YAML, JSON, shell) 10 / 10 0 / 10 0 / 10
Recording overhead on a 3m36s suite none measured +23 s -

Method, caveats and raw data: bench/RESULTS.md.

pip install testscope
pytest --testscope-record      # once (or in CI on main): full run + dependency map
# ... edit code ...
pytest --testscope             # only affected tests

When Claude Code runs pytest (it sets CLAUDECODE=1) and a map exists, selection turns on automatically. The agent needs no new command and no hook. For other agents, export TESTSCOPE=on in their environment. TESTSCOPE=off pytest always runs everything. Explicit targets (pytest tests/test_x.py::test_y) are never narrowed.

What it tracks

Dependency How testmon import graph
Functions a test runs sys.monitoring PY_START, disabled after the first hit, re-armed per test yes (coverage) no
Code run in child Python processes sitecustomize on PYTHONPATH, tagged with the running test no no
Data files read (YAML, JSON, fixtures, shell scripts run) audit hooks: open, subprocess.Popen no no
Files added to / removed from a listed directory audit hooks: os.listdir, os.scandir no no
Module-level code (constants, decorators, class bodies) by name: tests that ran code reading the changed name, in this module or its importers; plus up to 5 tests that import the module, to catch import crashes yes yes
Data files named by other files (a script that sources _lib.sh, a config that names a fixture) one hop from files tests read; chains only through shell scripts no no
Config / lockfile change (pyproject.toml, *.lock, requirements*.txt, …) full run full run no
Tests that failed last run always rerun yes no

Fingerprints are token-stream hashes without line numbers or comments, so moving code or changing a comment selects nothing. The map is a baseline. Selection compares the working tree with it by content, so it works on uncommitted edits and on branches built from an older main. As you edit, the selection can only grow. Re-record to reset it.

CI

examples/github-actions.yml:

  • push to main: pytest --testscope-record; the map goes into the Actions cache.
  • pull requests: restore the newest map; pytest --testscope.
  • merge queue + nightly: plain full run. This is the safety net for what any selector can miss.

Limits

  • Recording needs Python 3.12+ (sys.monitoring). Selection works on any 3.9+.
  • A test that starts a child process with a scrubbed environment (env={}), or with python -I / -E, hides that child's deps. Shell scripts that read files themselves are seen only as "script ran".
  • Network services, databases and time are not dependencies. A flaky test stays flaky.
  • Installing or upgrading a package is not detected. Re-record after dependency changes (a lockfile or requirements*.txt edit already triggers a full run).
  • A module-level edit runs at most 5 "import canary" tests for crashes at import time.
  • eval/exec strings and other dynamic code that no recorded test reached cannot be mapped.

Evidence

bench/RESULTS.md: real commits and fault injection on one 6,195-test project. For each injected fault, a full-suite run gives the ground truth. Missed files are rerun to rule out flaky tests. The harness is in bench/, so you can run it on your own suite.

Metadata

Release files for testscope 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for testscope 0.1.0
File Size Uploaded Provenance Actions
testscope-0.1.0.tar.gz 116.4 kB Without provenance Details for testscope-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for testscope 0.1.0
File Interpreter ABI Platform Actions
testscope-0.1.0-py3-none-any.whl Python 3 none any Details for testscope-0.1.0-py3-none-any.whl

Total release size: 137.3 kB

Release files / testscope-0.1.0.tar.gz

Download URL testscope-0.1.0.tar.gz
Size 116.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ba63bc30fb9b44544f38f72011d1a54bb53ebffcffb7fca147d573877c398141
BLAKE2b-256 checksum
How to use checksums
3f564b1619b79c8ff1373a23b2e9e7eb9efe8a56643ebbc8299864a13070c766
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.11

Release files / testscope-0.1.0-py3-none-any.whl

Download URL testscope-0.1.0-py3-none-any.whl
Size 20.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
124fead9ec9e479d267979c423903cc310f784c71db06cfec15d2a81ab9e3a6c
BLAKE2b-256 checksum
How to use checksums
c08705ce79455aa6207f5f868289e34411712a8256528914000d038b267d3849
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.11

Security for testscope 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page