testscope 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- testscope-0.1.0/.gitignore +5 -0
- testscope-0.1.0/LICENSE +201 -0
- testscope-0.1.0/PKG-INFO +99 -0
- testscope-0.1.0/README.md +83 -0
- testscope-0.1.0/assets/demo.gif +0 -0
- testscope-0.1.0/assets/selection.svg +14 -0
- testscope-0.1.0/bench/RESULTS.md +75 -0
- testscope-0.1.0/bench/commits.py +49 -0
- testscope-0.1.0/bench/data/FINAL_func_data.jsonl +45 -0
- testscope-0.1.0/bench/data/FINAL_mod.jsonl +9 -0
- testscope-0.1.0/bench/data/MERGED_commits.jsonl +31 -0
- testscope-0.1.0/bench/demo.tape +21 -0
- testscope-0.1.0/bench/module_mutants.py +36 -0
- testscope-0.1.0/bench/mutants.py +31 -0
- testscope-0.1.0/bench/report.py +77 -0
- testscope-0.1.0/bench/reselect.py +35 -0
- testscope-0.1.0/bench/results.json +135 -0
- testscope-0.1.0/bench/run.py +84 -0
- testscope-0.1.0/bench/static_sel.py +35 -0
- testscope-0.1.0/bench/verify.py +48 -0
- testscope-0.1.0/examples/github-actions.yml +38 -0
- testscope-0.1.0/pyproject.toml +28 -0
- testscope-0.1.0/src/testscope/__init__.py +0 -0
- testscope-0.1.0/src/testscope/_boot/sitecustomize.py +61 -0
- testscope-0.1.0/src/testscope/fingerprint.py +210 -0
- testscope-0.1.0/src/testscope/plugin.py +268 -0
- testscope-0.1.0/src/testscope/recorder.py +86 -0
- testscope-0.1.0/src/testscope/selection.py +267 -0
- testscope-0.1.0/tests/test_e2e.py +169 -0
- testscope-0.1.0/tests/test_fingerprint.py +24 -0
testscope-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Prismor
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
testscope-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: testscope
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Run only the tests a change can affect. Built for AI coding agents and CI.
|
|
5
|
+
Project-URL: Homepage, https://github.com/Ar9av/testscope
|
|
6
|
+
Project-URL: Issues, https://github.com/Ar9av/testscope/issues
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: ai agents,ci,claude code,pytest,regression test selection,test impact analysis
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Software Development :: Testing
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Requires-Dist: pytest>=7
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# testscope
|
|
18
|
+
|
|
19
|
+
Run only the tests your change can affect. Built for AI coding agents and CI.
|
|
20
|
+
|
|
21
|
+
Coding agents run `pytest` after every edit. On a 6,000-test suite that is minutes per
|
|
22
|
+
run. It goes past Claude Code's ~2-minute foreground limit, so the run moves to the
|
|
23
|
+
background and the agent stops reading its output. testscope records which code and
|
|
24
|
+
files each test really touches. After that, plain `pytest` runs only the tests your
|
|
25
|
+
change can reach.
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
On a real 6,195-test project, over its last 30 commits:
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
| | testscope | pytest-testmon | import graph |
|
|
34
|
+
|---|---|---|---|
|
|
35
|
+
| Median tests run per real commit (of 6,195) | **353** | 584 | 352 files of 378 |
|
|
36
|
+
| Injected faults caught (34 that break a test) | **33** | 22 | 22 |
|
|
37
|
+
| Faults in data files caught (YAML, JSON, shell) | **10 / 10** | 0 / 10 | 0 / 10 |
|
|
38
|
+
| Recording overhead on a 3m36s suite | none measured | +23 s | - |
|
|
39
|
+
|
|
40
|
+
Method, caveats and raw data: [`bench/RESULTS.md`](bench/RESULTS.md).
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install testscope
|
|
44
|
+
pytest --testscope-record # once (or in CI on main): full run + dependency map
|
|
45
|
+
# ... edit code ...
|
|
46
|
+
pytest --testscope # only affected tests
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
When Claude Code runs pytest (it sets `CLAUDECODE=1`) and a map exists, selection turns
|
|
50
|
+
on automatically. The agent needs no new command and no hook. For other agents, export
|
|
51
|
+
`TESTSCOPE=on` in their environment.
|
|
52
|
+
`TESTSCOPE=off pytest` always runs everything. Explicit targets
|
|
53
|
+
(`pytest tests/test_x.py::test_y`) are never narrowed.
|
|
54
|
+
|
|
55
|
+
## What it tracks
|
|
56
|
+
|
|
57
|
+
| Dependency | How | testmon | import graph |
|
|
58
|
+
|---|---|---|---|
|
|
59
|
+
| Functions a test runs | `sys.monitoring` PY_START, disabled after the first hit, re-armed per test | yes (coverage) | no |
|
|
60
|
+
| Code run in child Python processes | `sitecustomize` on `PYTHONPATH`, tagged with the running test | no | no |
|
|
61
|
+
| Data files read (YAML, JSON, fixtures, shell scripts run) | audit hooks: `open`, `subprocess.Popen` | no | no |
|
|
62
|
+
| Files added to / removed from a listed directory | audit hooks: `os.listdir`, `os.scandir` | no | no |
|
|
63
|
+
| 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 |
|
|
64
|
+
| 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 |
|
|
65
|
+
| Config / lockfile change (`pyproject.toml`, `*.lock`, `requirements*.txt`, …) | full run | full run | no |
|
|
66
|
+
| Tests that failed last run | always rerun | yes | no |
|
|
67
|
+
|
|
68
|
+
Fingerprints are token-stream hashes without line numbers or comments, so moving code or
|
|
69
|
+
changing a comment selects nothing. The map is a *baseline*. Selection compares
|
|
70
|
+
the working tree with it by content, so it works on uncommitted edits and on
|
|
71
|
+
branches built from an older main. As you edit, the selection can only grow.
|
|
72
|
+
Re-record to reset it.
|
|
73
|
+
|
|
74
|
+
## CI
|
|
75
|
+
|
|
76
|
+
[`examples/github-actions.yml`](examples/github-actions.yml):
|
|
77
|
+
|
|
78
|
+
- **push to main:** `pytest --testscope-record`; the map goes into the Actions cache.
|
|
79
|
+
- **pull requests:** restore the newest map; `pytest --testscope`.
|
|
80
|
+
- **merge queue + nightly:** plain full run. This is the safety net for what any selector can miss.
|
|
81
|
+
|
|
82
|
+
## Limits
|
|
83
|
+
|
|
84
|
+
- Recording needs Python 3.12+ (`sys.monitoring`). Selection works on any 3.9+.
|
|
85
|
+
- A test that starts a child process with a scrubbed environment (`env={}`), or with
|
|
86
|
+
`python -I` / `-E`, hides that child's deps. Shell scripts that read files themselves
|
|
87
|
+
are seen only as "script ran".
|
|
88
|
+
- Network services, databases and time are not dependencies. A flaky test stays flaky.
|
|
89
|
+
- Installing or upgrading a package is not detected. Re-record after dependency changes
|
|
90
|
+
(a lockfile or `requirements*.txt` edit already triggers a full run).
|
|
91
|
+
- A module-level edit runs at most 5 "import canary" tests for crashes at import time.
|
|
92
|
+
- `eval`/`exec` strings and other dynamic code that no recorded test reached cannot be mapped.
|
|
93
|
+
|
|
94
|
+
## Evidence
|
|
95
|
+
|
|
96
|
+
[`bench/RESULTS.md`](bench/RESULTS.md): real commits and fault injection on one 6,195-test
|
|
97
|
+
project. For each injected fault, a full-suite run gives the ground truth. Missed files are
|
|
98
|
+
rerun to rule out flaky tests. The harness is in [`bench/`](bench/), so you can run it on
|
|
99
|
+
your own suite.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# testscope
|
|
2
|
+
|
|
3
|
+
Run only the tests your change can affect. Built for AI coding agents and CI.
|
|
4
|
+
|
|
5
|
+
Coding agents run `pytest` after every edit. On a 6,000-test suite that is minutes per
|
|
6
|
+
run. It goes past Claude Code's ~2-minute foreground limit, so the run moves to the
|
|
7
|
+
background and the agent stops reading its output. testscope records which code and
|
|
8
|
+
files each test really touches. After that, plain `pytest` runs only the tests your
|
|
9
|
+
change can reach.
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
On a real 6,195-test project, over its last 30 commits:
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
| | testscope | pytest-testmon | import graph |
|
|
18
|
+
|---|---|---|---|
|
|
19
|
+
| Median tests run per real commit (of 6,195) | **353** | 584 | 352 files of 378 |
|
|
20
|
+
| Injected faults caught (34 that break a test) | **33** | 22 | 22 |
|
|
21
|
+
| Faults in data files caught (YAML, JSON, shell) | **10 / 10** | 0 / 10 | 0 / 10 |
|
|
22
|
+
| Recording overhead on a 3m36s suite | none measured | +23 s | - |
|
|
23
|
+
|
|
24
|
+
Method, caveats and raw data: [`bench/RESULTS.md`](bench/RESULTS.md).
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install testscope
|
|
28
|
+
pytest --testscope-record # once (or in CI on main): full run + dependency map
|
|
29
|
+
# ... edit code ...
|
|
30
|
+
pytest --testscope # only affected tests
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
When Claude Code runs pytest (it sets `CLAUDECODE=1`) and a map exists, selection turns
|
|
34
|
+
on automatically. The agent needs no new command and no hook. For other agents, export
|
|
35
|
+
`TESTSCOPE=on` in their environment.
|
|
36
|
+
`TESTSCOPE=off pytest` always runs everything. Explicit targets
|
|
37
|
+
(`pytest tests/test_x.py::test_y`) are never narrowed.
|
|
38
|
+
|
|
39
|
+
## What it tracks
|
|
40
|
+
|
|
41
|
+
| Dependency | How | testmon | import graph |
|
|
42
|
+
|---|---|---|---|
|
|
43
|
+
| Functions a test runs | `sys.monitoring` PY_START, disabled after the first hit, re-armed per test | yes (coverage) | no |
|
|
44
|
+
| Code run in child Python processes | `sitecustomize` on `PYTHONPATH`, tagged with the running test | no | no |
|
|
45
|
+
| Data files read (YAML, JSON, fixtures, shell scripts run) | audit hooks: `open`, `subprocess.Popen` | no | no |
|
|
46
|
+
| Files added to / removed from a listed directory | audit hooks: `os.listdir`, `os.scandir` | no | no |
|
|
47
|
+
| 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 |
|
|
48
|
+
| 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 |
|
|
49
|
+
| Config / lockfile change (`pyproject.toml`, `*.lock`, `requirements*.txt`, …) | full run | full run | no |
|
|
50
|
+
| Tests that failed last run | always rerun | yes | no |
|
|
51
|
+
|
|
52
|
+
Fingerprints are token-stream hashes without line numbers or comments, so moving code or
|
|
53
|
+
changing a comment selects nothing. The map is a *baseline*. Selection compares
|
|
54
|
+
the working tree with it by content, so it works on uncommitted edits and on
|
|
55
|
+
branches built from an older main. As you edit, the selection can only grow.
|
|
56
|
+
Re-record to reset it.
|
|
57
|
+
|
|
58
|
+
## CI
|
|
59
|
+
|
|
60
|
+
[`examples/github-actions.yml`](examples/github-actions.yml):
|
|
61
|
+
|
|
62
|
+
- **push to main:** `pytest --testscope-record`; the map goes into the Actions cache.
|
|
63
|
+
- **pull requests:** restore the newest map; `pytest --testscope`.
|
|
64
|
+
- **merge queue + nightly:** plain full run. This is the safety net for what any selector can miss.
|
|
65
|
+
|
|
66
|
+
## Limits
|
|
67
|
+
|
|
68
|
+
- Recording needs Python 3.12+ (`sys.monitoring`). Selection works on any 3.9+.
|
|
69
|
+
- A test that starts a child process with a scrubbed environment (`env={}`), or with
|
|
70
|
+
`python -I` / `-E`, hides that child's deps. Shell scripts that read files themselves
|
|
71
|
+
are seen only as "script ran".
|
|
72
|
+
- Network services, databases and time are not dependencies. A flaky test stays flaky.
|
|
73
|
+
- Installing or upgrading a package is not detected. Re-record after dependency changes
|
|
74
|
+
(a lockfile or `requirements*.txt` edit already triggers a full run).
|
|
75
|
+
- A module-level edit runs at most 5 "import canary" tests for crashes at import time.
|
|
76
|
+
- `eval`/`exec` strings and other dynamic code that no recorded test reached cannot be mapped.
|
|
77
|
+
|
|
78
|
+
## Evidence
|
|
79
|
+
|
|
80
|
+
[`bench/RESULTS.md`](bench/RESULTS.md): real commits and fault injection on one 6,195-test
|
|
81
|
+
project. For each injected fault, a full-suite run gives the ground truth. Missed files are
|
|
82
|
+
rerun to rule out flaky tests. The harness is in [`bench/`](bench/), so you can run it on
|
|
83
|
+
your own suite.
|
|
Binary file
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 210" width="720" height="210" font-family="-apple-system,Segoe UI,Helvetica,Arial,sans-serif">
|
|
2
|
+
<style>.bg{fill:#fcfcfb}.t1{fill:#0b0b0b}.t2{fill:#52514e}.muted{fill:#c9c7c0}.acc{fill:#2a78d6}.grid{stroke:#e6e4de}@media (prefers-color-scheme:dark){.bg{fill:#1a1a19}.t1{fill:#f0efec}.t2{fill:#c3c2b7}.muted{fill:#55544f}.acc{fill:#3987e5}.grid{stroke:#33332f}}</style>
|
|
3
|
+
<rect class="bg" width="720" height="210" rx="8"/>
|
|
4
|
+
<text class="t1" x="20" y="28" font-size="15" font-weight="600">Tests run per change, median of 30 real commits</text>
|
|
5
|
+
<text class="t2" x="108" y="72.0" font-size="13" text-anchor="end">Full suite</text>
|
|
6
|
+
<rect class="muted" x="120" y="52" width="510.0" height="30" rx="4"/>
|
|
7
|
+
<text class="t1" x="638.0" y="72.0" font-size="13" font-weight="600">6,195</text>
|
|
8
|
+
<text class="t2" x="108" y="118.0" font-size="13" text-anchor="end">testmon</text>
|
|
9
|
+
<rect class="muted" x="120" y="98" width="48.1" height="30" rx="4"/>
|
|
10
|
+
<text class="t1" x="176.1" y="118.0" font-size="13" font-weight="600">584</text>
|
|
11
|
+
<text class="t2" x="108" y="164.0" font-size="13" text-anchor="end">testscope</text>
|
|
12
|
+
<rect class="acc" x="120" y="144" width="29.1" height="30" rx="4"/>
|
|
13
|
+
<text class="t1" x="157.1" y="164.0" font-size="13" font-weight="600">353</text>
|
|
14
|
+
</svg>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Results
|
|
2
|
+
|
|
3
|
+
Measured on one real project: a Python codebase with 6,195 tests in 378 test files
|
|
4
|
+
(pytest, 12 xdist workers). The full suite takes 3m36s. Raw rows are in [`data/`](data/),
|
|
5
|
+
and the summary is in [`results.json`](results.json).
|
|
6
|
+
|
|
7
|
+
## Real commits
|
|
8
|
+
|
|
9
|
+
The last 30 non-merge commits that touch code, each reverse-applied to the working tree
|
|
10
|
+
against a map recorded at HEAD ([`commits.py`](commits.py)).
|
|
11
|
+
|
|
12
|
+
| | median tests | mean tests | median test files | selection time (median) |
|
|
13
|
+
|---|---|---|---|---|
|
|
14
|
+
| Full suite | 6,195 | 6,195 | 378 | - |
|
|
15
|
+
| testscope | 353 | 652 | 54.5 | 4.9 s |
|
|
16
|
+
| pytest-testmon | 584 | 836 | 76 | 3.3 s |
|
|
17
|
+
| Import graph (transitive importers) | - | - | 352 | - |
|
|
18
|
+
|
|
19
|
+
The testscope numbers include 34 tests that failed at record time in that checkout
|
|
20
|
+
(environment failures). testscope always reruns the tests that failed last time. Without
|
|
21
|
+
them, testscope selects fewer tests than testmon on 21 of 30 commits (1 tie).
|
|
22
|
+
|
|
23
|
+
The testmon numbers come from an earlier run on the same 30 commits. In the final run,
|
|
24
|
+
one extra package was installed in the venv. testmon fingerprints the installed packages,
|
|
25
|
+
so it then selected the full suite on every commit. That is documented testmon
|
|
26
|
+
behavior, and the final run is not a fair measure of it.
|
|
27
|
+
|
|
28
|
+
## Fault injection
|
|
29
|
+
|
|
30
|
+
Each fault is put into the working tree. A full-suite run gives the ground truth (the
|
|
31
|
+
test files that fail). Then each selector is asked which files to run.
|
|
32
|
+
|
|
33
|
+
- **py**: `raise RuntimeError` as the first line of a function ([`mutants.py`](mutants.py)).
|
|
34
|
+
- **module**: a module-level constant set to `None` ([`module_mutants.py`](module_mutants.py)).
|
|
35
|
+
- **data**: a YAML, JSON, shell or fixture file replaced with garbage.
|
|
36
|
+
|
|
37
|
+
"Caught" means the selection includes at least one failing file, so the run goes red.
|
|
38
|
+
"All files" means it includes every failing file.
|
|
39
|
+
|
|
40
|
+
| kind | breaking faults | testscope caught / all files | testmon caught / all files | import graph caught / all files |
|
|
41
|
+
|---|---|---|---|---|
|
|
42
|
+
| py | 20 | 20 / 19 | 18 / 15 | 18 / 17 |
|
|
43
|
+
| module | 4 | 3 / 3 | 4 / 4 | 4 / 4 |
|
|
44
|
+
| data | 10 | 10 / 10 | 0 / 0 | 0 / 0 |
|
|
45
|
+
| **all** | **34** | **33 / 32** | **22 / 19** | **22 / 21** |
|
|
46
|
+
|
|
47
|
+
Median files selected per fault: testscope 7, testmon 5, import graph 352.
|
|
48
|
+
|
|
49
|
+
Notes:
|
|
50
|
+
|
|
51
|
+
- The module miss (`A2A_SCREENED_METHODS = None`) crashes an import. The testscope
|
|
52
|
+
selection does go red, through a collection error in a test file that imports the
|
|
53
|
+
module. The harness counts collection errors as a baseline bucket, so it cannot credit
|
|
54
|
+
this case. We report it as a miss.
|
|
55
|
+
- 54 faults were run. 20 broke no test. 3 more module faults made the suite hang and were
|
|
56
|
+
skipped.
|
|
57
|
+
- Flaky tests: each missed file was rerun alone, 2 times with the fault and 2 times
|
|
58
|
+
without ([`verify.py`](verify.py)). A miss counts only if the file fails 2/2 with the
|
|
59
|
+
fault and passes 2/2 clean. Files that are flaky are removed from the ground truth
|
|
60
|
+
for all selectors.
|
|
61
|
+
|
|
62
|
+
## Overhead
|
|
63
|
+
|
|
64
|
+
| | time |
|
|
65
|
+
|---|---|
|
|
66
|
+
| Plain full run | 3m36s |
|
|
67
|
+
| `pytest --testscope-record` | 3m24s |
|
|
68
|
+
| testmon DB build | 3m59s |
|
|
69
|
+
|
|
70
|
+
Recording adds no measurable cost (run-to-run noise is larger).
|
|
71
|
+
|
|
72
|
+
## Limits of this evidence
|
|
73
|
+
|
|
74
|
+
One project. Faults are synthetic. Real commits are measured for size, not for misses.
|
|
75
|
+
Run the harness on your own suite before you trust it there.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Selection size on real changes: for each recent commit touching source .py files,
|
|
2
|
+
reverse-apply it onto HEAD (same functions as the real change) and count what each
|
|
3
|
+
selector picks. Needs .testscope/map.json and .testmondata recorded at HEAD.
|
|
4
|
+
Usage: python commits.py <repo> <n> <out.jsonl>"""
|
|
5
|
+
import json, os, subprocess, sys, time
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
repo, n, out = Path(sys.argv[1]), int(sys.argv[2]), Path(sys.argv[3])
|
|
9
|
+
here = Path(__file__).parent
|
|
10
|
+
SEL = {
|
|
11
|
+
"testscope": "TESTSCOPE=on python -m pytest -p no:testmon -p no:cacheprovider --collect-only -q tests",
|
|
12
|
+
"testmon": "TESTSCOPE=off python -m pytest -p no:testscope --testmon-nocollect -p no:cacheprovider --collect-only -q tests",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def sh(cmd, **kw):
|
|
17
|
+
return subprocess.run(cmd, shell=True, cwd=repo, capture_output=True, text=True, **kw)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def count(cmd):
|
|
21
|
+
t = time.time()
|
|
22
|
+
lines = [l for l in sh(cmd + " 2>/dev/null").stdout.splitlines() if "::" in l]
|
|
23
|
+
return len(lines), len({l.split("::")[0] for l in lines}), round(time.time() - t, 1)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
total = count("TESTSCOPE=off python -m pytest -p no:testmon -p no:testscope -p no:cacheprovider --collect-only -q tests")
|
|
27
|
+
shas = sh(f"git log --format=%h -n {n * 3} HEAD -- 'prismor/*.py' 'prismor/**/*.py'").stdout.split()[1:]
|
|
28
|
+
done = 0
|
|
29
|
+
with out.open("w") as fh:
|
|
30
|
+
fh.write(json.dumps({"total_tests": total[0], "total_files": total[1]}) + "\n")
|
|
31
|
+
for c in shas:
|
|
32
|
+
if done >= n:
|
|
33
|
+
break
|
|
34
|
+
if sh(f"git show --format= {c} -- prismor | git apply -R --check").returncode:
|
|
35
|
+
continue
|
|
36
|
+
sh(f"git show --format= {c} -- prismor | git apply -R")
|
|
37
|
+
try:
|
|
38
|
+
files = sh("git diff --name-only").stdout.split()
|
|
39
|
+
row = {"commit": c, "files": len(files)}
|
|
40
|
+
for k, cmd in SEL.items():
|
|
41
|
+
row[k] = count(cmd)
|
|
42
|
+
imp = sh(f"python {here}/static_sel.py imports").stdout.split()
|
|
43
|
+
row["imports"] = (None, len(imp), None)
|
|
44
|
+
finally:
|
|
45
|
+
sh("git checkout -- .")
|
|
46
|
+
fh.write(json.dumps(row) + "\n")
|
|
47
|
+
fh.flush()
|
|
48
|
+
print(row, flush=True)
|
|
49
|
+
done += 1
|