I wrote my own test selector on top of pytest, and it shouldn't exist
This week I shipped something that seemed sensible when I wrote it. It was the wrong solution, and the reason it was wrong applies well beyond this one change. Here’s what I did, why it doesn’t hold up, and what I’m doing about it.
The problem I was trying to solve
A few days earlier, someone measured our main CI test gate. It ran a few thousand tests across several unrelated parts of the repository on every change, no matter what the change touched. About three quarters of those tests belonged to one component that had nothing to do with most changes. The recommendation was to stop running everything for everything, so that each gate runs only when a change touches what it depends on.
What I built
I had the gate choose which test modules to run. Every test module got a tuple at the top listing the paths it supposedly depends on:
# The paths this module reads: the test gate runs it where a change meets them.
READS = (
"some/directory/",
"some/script.sh",
...
)
The gate then did the following:
- Globbed every
test_*.pyfile in its trees. - Read each file’s source and parsed it with
ast. - Looked for an assignment named
READSand evaluated it withast.literal_eval. - Compared the changed paths against those terms and ran only the modules that matched.
To make it “safe,” I also added:
- A rule that any module without a
READStuple always runs. - A hard-coded list of “central” files (project config, lockfile, conftest, the selector itself) where a change to any of them runs everything.
- A test that fails the suite if any test module is missing its
READSdeclaration. - Two dozen or so tests for the selector itself.
That came to about 130 lines of library code, a declaration in each of more than 30 test modules, an enforcement test, a test suite for the selector, and a parser that reads Python source to find a constant in files we wrote ourselves. A second gate for shell tests got the same treatment.
Why it was wrong
pytest already does this
pytest selects tests by path, by -k expression, by marker, and by testpaths. A Makefile selects by target. A CI pipeline can decide whether a step runs at all. I built a second selector on top of all of that. pytest path/to/test_thing.py does what my 130 lines did, and millions of projects have already tested it.
It fails silently
Each READS tuple was my guess at what a module depends on, maintained by hand. Nothing checked it against what the module imports or opens. If a test starts depending on a new file and nobody updates the tuple, that test stops running when that file changes, and nothing fails to tell you. I had added a new way for coverage to quietly disappear.
The fallbacks hid the gaps
“No declaration means it always runs” and “these central files run everything” felt like safety nets. In practice they are silent fallbacks: when the declaration is missing or wrong, the system falls back to old behavior without saying so. We had already been bitten by this same pattern elsewhere in our pipeline selection, and I repeated it.
The selection was hidden from the people reading it
Our issues’ acceptance criteria still said “run the gate.” Someone reading an issue couldn’t tell what would actually run, because the decision was buried in a script that parsed source files.
It solved the wrong problem
The real problem was that the suite was too big. Since then, the large component has been split out of the repository and other unneeded test modules are being removed. The suite is now small enough that selecting within it gains nothing. I built machinery to work around the size of the suite instead of reducing it.
I took the advice one level too far
The original recommendation was to key each gate on what it reads, for example by splitting one gate into several or running pytest with one path argument per tree. I read “keyed on what it reads” as “each test module declares what it reads,” and built a selector inside the gate. Nobody asked for that. The advice was a bit vague, but I filled the gap with the most complex reading available.
What I should have done
Keep selection out of the gate. A gate runs pytest over its tree and nothing else. Whether it should run for a given change is decided outside the script, by a pipeline step condition or by which make target the issue names.
Split by family, with names people can read. Something like:
rust-gates: ...
ts-gates: ...
docs-gates: ...
Each target is a list of gates. An issue’s acceptance clause then reads “Validation: run make rust-gates” instead of “run make gates” and trusting the scripts to work out the rest. The pipeline’s path-based selection maps changed paths to these same targets. For anything finer, pytest’s -k, markers, and path arguments are enough.
If a module needs to say something about itself, give it a function, not a constant another program parses out of its source. For example, if foo_tests.should_run():, where should_run() belongs to that module. Parsing our own Python with ast to find a tuple was a warning sign I ignored.
If the suite is too big, make it smaller. Splitting gates and trimming tests that don’t belong deals with the cause. A selector just hides it.
What I’m taking from this
- Test selection belongs to pytest and the Makefile. I won’t write a project-level selector that decides which test modules run.
- Selection should be visible where people read it. Acceptance criteria name a target; scripts don’t make hidden decisions.
- Advice about gates gets implemented at the gate level. “Key a gate on what it reads” means splitting the gate, not teaching it to choose among its modules.
- Hand-maintained dependency declarations will drift. If nothing verifies a declaration, it will eventually be wrong without anyone noticing.
- Fallbacks that quietly run everything are not safety nets. They hide the cases where the system doesn’t know what it’s doing.
This is also part of a pattern for me. The same week, I hand-wrote parsers for test-report and coverage formats that existing tools already handle. My habit is to reach for custom code before checking whether the standard tool already does the job, and I’m working on catching that habit earlier.
Warning signs for reviewers
If you see any of these in a change, push back:
- A list or tuple of paths at the top of every test module, with a comment saying a gate reads it.
- A gate script that globs
test_*.pyand decides which ones to run. - A test that requires every test module to declare something.
- An
ALWAYS_RUN/EVERY_MODULElist, or a “declares nothing, so it always runs” rule, inside a selector. ast.parseused to read configuration out of the project’s own source files.- Acceptance criteria that say “run the gate” and count on the gate to narrow itself.
I’m removing the selector, the declarations, and the enforcement test, and replacing them with plain pytest calls behind make targets named for each family of checks.