Skip to content

ci: parallelize pytest with pytest-xdist - #706

Merged
anshss merged 3 commits into
mainfrom
ci-parallel
Aug 7, 2026
Merged

anshss merged 3 commits into
mainfrom
ci-parallel

Conversation

@anshss

@anshss anshss commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Parallelizes the CI test job with pytest-xdist, and fixes an order-dependent test found along the way that would otherwise have looked like xdist-caused flakiness.

Summary

  • Added pytest-xdist to the dev extra in pyproject.toml.
  • .github/workflows/ci.yml's test job now runs pytest -n auto tests/unit/ tests/synthetic/ tests/agents/ tests/integration/, fanning the suite out across available cores instead of running it single-threaded.
  • Fixed 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.
  • Updated .claude/rules/release.md's CI description to match; left the Makefile/CONTRIBUTING.md local pytest invocations 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.duckdb via Path(...).expanduser(). On this codebase's Python, Path.expanduser() reads the HOME environment variable directly — it does not go through pathlib.Path.home().

The test patched pathlib.Path.home() to point at its tmp_path, expecting that to redirect _db_has_data()'s resolution there. Since expanduser() never calls Path.home(), that patch was a no-op: _db_has_data() kept resolving against the real HOME env var, which in this suite is the session-wide fake home that tests/conftest.py's autouse _tj_isolated_home fixture 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 own tmp_path. Run alone, nothing had populated that shared path, so the assertion failed.

Fix: the test now sets HOME itself via monkeypatch.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:

tests/unit/test_onboard_first_run.py::test_home_when_db_populated_but_no_config_is_set_up PASSED

Its file alone: 19 passed in 2.66s.

Full suite, serial (before landing -n auto):

5413 passed, 17 skipped, 2 xfailed, 1 warning in 312.55s (0:05:12)

Full suite, with -n auto (12-core machine):

5413 passed, 17 skipped, 2 xfailed, 12 warnings in 52.80s

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

  • No other tests needed isolation fixes — the order-dependent test above was the only one that surfaced under either serial-alone or parallel runs.
  • Local dev invocations (Makefile, CONTRIBUTING.md) intentionally keep plain serial pytest — -n auto adds 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

anshss and others added 2 commits August 7, 2026 11:06
… 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]>
@anshss
anshss requested a review from anilmurty as a code owner August 7, 2026 05:39
@greptile-apps

greptile-apps Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR parallelizes the Python CI suite with pytest-xdist and corrects a test whose database-path isolation previously depended on suite order.

  • Adds pytest-xdist to the development dependencies.
  • Runs the Python CI test matrix with pytest -n auto.
  • Redirects the onboarding test’s HOME so Path.expanduser() resolves its test-local database.
  • Updates contributor-facing CI documentation and records the path-resolution testing rule.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

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

@anshss
anshss merged commit 7bffe76 into main Aug 7, 2026
6 checks passed
@anilmurty
anilmurty deleted the ci-parallel branch September 18, 2026 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant