Repository navigation
ci: parallelize pytest with pytest-xdist - #706
Merged
Merged
Conversation
… HOME test_home_when_db_populated_but_no_config_is_set_up wrote its populated DB under tmp_path but patched pathlib.Path.home() to point _db_has_data() at it. Path.expanduser() (which _db_has_data() actually calls to resolve the default "~/.tj/telemetry.duckdb") reads the HOME env var directly rather than going through Path.home(), so that patch was a no-op: the test only passed when an earlier test in the suite happened to leave a non-empty DB under the session-wide fake HOME the conftest.py autouse fixture sets up. Run alone, nothing had populated that shared path, and the assertion failed. Fix the test to set HOME itself (the codebase's existing pattern for this, per test_span_pricing.py / test_humanize.py / test_doctor_paths.py) so its own DB is what gets found, independent of suite order or other tests' side effects. Co-Authored-By: Claude <[email protected]>
The full serial suite (tests/unit/, tests/synthetic/, tests/agents/, tests/integration/) takes over 5 minutes, most of it wall-clock idle while a single worker churns through 5000+ tests. Add pytest-xdist to the dev extra and pass -n auto to the CI test job so it fans out across CPU cores instead. Verified before landing: ran the full suite serially and with -n auto back to back. Both report identical results (5413 passed, 17 skipped, 2 xfailed) — no shared-state flakiness surfaced under parallel execution, so no test isolation fixes were needed here (unlike the order-dependent test fixed in the prior commit). Serial wall clock ~313s vs ~53s under -n auto on a 12-core machine. Also updated .claude/rules/release.md's CI description to match, and left the Makefile/CONTRIBUTING.md local pytest invocations serial — parallelism only pays off at the full-suite scale CI runs, not for a single narrow test during local debugging. Co-Authored-By: Claude <[email protected]>
Contributor
|
| Filename | Overview |
|---|---|
| .github/workflows/ci.yml | Enables automatic pytest worker parallelism for the existing Python test directories. |
| pyproject.toml | Adds pytest-xdist to the development extra used by CI. |
| tests/unit/test_onboard_first_run.py | Replaces an ineffective Path.home patch with test-local HOME isolation for expanduser-based database discovery. |
| CLAUDE.md | Documents the expanduser and HOME isolation invariant exposed by the corrected test. |
| .claude/rules/release.md | Updates the CI documentation to describe parallel Python test execution. |
Reviews (2): Last reviewed commit: "docs: record that expanduser() reads $HO..." | Re-trigger Greptile
Co-Authored-By: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Parallelizes the CI
testjob withpytest-xdist, and fixes an order-dependent test found along the way that would otherwise have looked like xdist-caused flakiness.Summary
pytest-xdistto thedevextra inpyproject.toml..github/workflows/ci.yml'stestjob now runspytest -n auto tests/unit/ tests/synthetic/ tests/agents/ tests/integration/, fanning the suite out across available cores instead of running it single-threaded.tests/unit/test_onboard_first_run.py::test_home_when_db_populated_but_no_config_is_set_up, which passed in a full-suite run but failed standalone — a real order dependency, not something xdist introduced..claude/rules/release.md's CI description to match; left the Makefile/CONTRIBUTING.md localpytestinvocations serial (parallelism only pays off at CI's full-suite scale, not a single test during local debugging).The order-dependent test — root cause
_db_has_data()(tokenjam/cli/home.py) resolves the default DB path~/.tj/telemetry.duckdbviaPath(...).expanduser(). On this codebase's Python,Path.expanduser()reads theHOMEenvironment variable directly — it does not go throughpathlib.Path.home().The test patched
pathlib.Path.home()to point at itstmp_path, expecting that to redirect_db_has_data()'s resolution there. Sinceexpanduser()never callsPath.home(), that patch was a no-op:_db_has_data()kept resolving against the realHOMEenv var, which in this suite is the session-wide fake home thattests/conftest.py's autouse_tj_isolated_homefixture sets up once for the whole run.The test only ever passed by accident: in a full-suite run, some earlier test had already left a non-empty DB file under that shared session-wide fake
HOME, which is what the assertion was actually seeing — not the DB this test created under its owntmp_path. Run alone, nothing had populated that shared path, so the assertion failed.Fix: the test now sets
HOMEitself viamonkeypatch.setenv("HOME", str(tmp_path))— the pattern already used elsewhere in the suite (test_span_pricing.py,test_humanize.py,test_doctor_paths.py) — so its own DB is what_db_has_data()actually finds, independent of suite order or another test's side effects. The assertion itself ("You're set up" in out) is unchanged.Tests / Verification
Fixed test, in isolation:
Its file alone:
19 passed in 2.66s.Full suite, serial (before landing
-n auto):Full suite, with
-n auto(12-core machine):Identical pass/skip/xfail counts between serial and parallel runs — no test failed only under
-n auto, so no masking (--dist loadfile, deselection, xfail) was needed anywhere in the suite.What's NOT in this PR
Makefile,CONTRIBUTING.md) intentionally keep plain serialpytest—-n autoadds startup overhead that isn't worth it for a single narrow test during local debugging, only for the full suite CI runs.🤖 Generated with Claude Code