testscope

agent
Guvenlik Denetimi
Gecti
Health Gecti
  • License — License: Apache-2.0
  • Description — Repository has a description
  • Active repo — Last push 0 days ago
  • Community trust — 13 GitHub stars
Code Gecti
  • Code scan — Scanned 12 files during light audit, no dangerous patterns found
Permissions Gecti
  • Permissions — No dangerous permissions requested

Bu listing icin henuz AI raporu yok.

SUMMARY

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

README.md

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
Bad package upgrades caught (4 that break a test) 4, running 92 to 2,278 tests 4, running all 6,195 0
Mean time to first failure with -x (same selection) 4.0 s fail-first; 21.3 s in pytest's order not measured -
Recording overhead on a 3m36s suite none measured +23 s -
Selection time for a one-line change (testscope logic only) 0.13 s not measured -

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

Quick start

  1. Install it as a dev dependency: pip install testscope (or uv add --dev testscope).

  2. Add .testscope/ to .gitignore. The map is local state; do not commit it.

  3. Record the map once. This is one full run (Python 3.12+):

    pytest --testscope-record
    
  4. Edit code, then run only the affected tests:

    pytest --testscope
    

The header line testscope: ... tells you what was selected and why.
.testscope/last-selection.json gives the reason for each test.

Agents. When an agent runs pytest and a map exists, selection turns on by itself. The
agent needs no new command and no hook. testscope detects Claude Code (CLAUDECODE),
Codex (CODEX_SANDBOX, CODEX_THREAD_ID), Cursor (CURSOR_AGENT) and Gemini CLI
(GEMINI_CLI). For other agents, set TESTSCOPE=on in their environment.

Always: TESTSCOPE=off pytest runs everything. Named targets
(pytest tests/test_x.py::test_y) are never narrowed.

Set it up with an agent. Tell your coding agent: "Set up testscope in this repo,
following https://github.com/Ar9av/testscope/blob/master/AGENTS.md"
. That file has the
steps, the checks, and a block of text to add to your project's own AGENTS.md /
CLAUDE.md.

A git worktree with no map of its own uses the main checkout's map. When a change forces a
full run (a config or lockfile changed), testscope records a fresh map during that run, so
the next run is narrow again. A run that covers less than 90% of the old map's tests (-k,
-x, a subdirectory) keeps the old map. TESTSCOPE_AUTORECORD=off turns this off.

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
Installed packages (upgrade, downgrade, removal) the packages whose code each test ran, with their versions; a version change reruns only those tests full run no
Tests that failed last run always rerun, and run first yes no

Selected tests run in fail-first order: tests that failed last time, then tests that ran
the changed code, then indirect reasons; shorter files first (longer first under xdist). Whole files move, so module
fixtures still run once. TESTSCOPE_ORDER=off keeps pytest's order.

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.

A test that passed does not run again

(0.5.0 and later.) Each selective run also records what its passing tests touched, and the code they passed
on (.testscope/verified.json). On the next run, an affected test is skipped if nothing
it depends on has changed since a run where it passed. So the cost follows your last
edit, not everything since the map. Going back to an earlier passing state (undo, or a
branch switch) also runs nothing.

Only a test body that ran and passed is remembered. Failures run again, and so do skipped
tests, because a skip can depend on the machine. TESTSCOPE_INCREMENTAL=off turns this
off. Recording a new map clears it. It needs Python 3.12+. The extra recording stayed within
run-to-run noise (about ±20% on that machine).

The check: over 15 commits of a 7,700-test project, every skipped test was run again by
force (18,798 runs). 18,796 passed. The other 2, one of them a wall-clock assertion,
passed on each repeat at the same commit.

In CI, cache .testscope/map.json only, as the example workflow does. A cached
verified.json would let one job skip a test because an earlier job passed it.

Like the map, this only knows what testscope can see. A test that depends on time, the
network or load can pass once and be skipped afterwards.

Speed

  • With pytest-xdist, the selection is computed once and sent to the workers.
  • -n auto is lowered to fit the selection: no workers at all for under 5 s of tests.
    An explicit -n 8 is left as it is. TESTSCOPE_XDIST=keep turns this off.
  • The map stores each file's imports, so unchanged files are not parsed again.
  • Under xdist, slow files start first within each fail-first rank.

A map recorded by 0.4.x still works, but selects at the old speed until you record again.

Wall time on that 7,700-test project, -n auto on 14 cores, median of 3:

Change 0.4.2 0.5.0, first run 0.5.0, same code again
One function edited 1.9 s 0.9 s 0.6 s
Module-level line in a hot file 13.6 s 1.8 s 0.6 s
A commit that selects 355 tests 117 s 111 s 4 s
A commit that selects 2,742 tests 138 s 120 s 9 s

Selection alone, replayed over 24 whole commits of that project, fell from 1.70 s to 0.18 s
on average, with identical picks.

When many tests are selected, the tests themselves are the cost: the first run gains little.

No map yet? (optional AI pick)

TESTSCOPE_AI=claude pytest          # your coding agent's own login, no API key
pip install 'testscope[ai]' && TESTSCOPE_AI=anthropic pytest   # or the API, ANTHROPIC_API_KEY
TESTSCOPE_AI= runs auth
claude claude -p Claude Code login
codex codex exec Codex login (ChatGPT plan) or API key
cursor cursor-agent -p Cursor login
gemini gemini -p Google login or GEMINI_API_KEY
copilot copilot -p Copilot login
qwen qwen Qwen Code's own auth
opencode opencode run any provider opencode is set up with
anthropic Anthropic API ANTHROPIC_API_KEY

Each CLI runs read-only in an empty directory, so it sees no project files. TESTSCOPE_AI_MODEL
picks the model. Measured: Claude, Codex and opencode. Cursor, Gemini, Copilot and Qwen are
tested against their documented output only.

Until a map is recorded, a model reads the diff and a test index and picks the test files
to run. On the same faults it caught 33 of 34 while it ran about 3 files, for about 1 cent a
call on Opus 5.5 (0.05 cents on Haiku 5.5). It is a guess: it found every failing file only
21 times in 34. Once a map exists the AI step is off. (We measured it on top of the map
too: it caught nothing extra, so that mode was removed.) Details:
bench/RESULTS.md.

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.
  • Package tracking sees Python code that runs inside a package. A package that is only a
    compiled extension, with no Python layer, is invisible; a lockfile or
    requirements*.txt edit still 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.

Yorumlar (0)

Sonuc bulunamadi