Skip to content

feat: run pytest in parallel with pytest-xdist by default - #36

Merged
mjun0812 merged 1 commit into
mainfrom
feat/pytest-xdist
Sep 7, 2026
Merged

mjun0812 merged 1 commit into
mainfrom
feat/pytest-xdist

Conversation

@mjun0812

@mjun0812 mjun0812 commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Overview and Background

Generated projects now run their test suite in parallel by default: pytest distributes tests across all CPU cores through pytest-xdist. Previously tests ran serially, so suites that grow beyond the template's three example tests paid the full wall-clock cost in CI and in agent-driven edit/test loops.

Related Issues

None

Implementation Approach

  • pytest-xdist>=3.8.0 joins the dev dependency group and uv.lock is regenerated (adds pytest-xdist and its execnet dependency).
  • --numprocesses=auto is appended to [tool.pytest] addopts, so it applies to every invocation without changing the CI commands. The long option name is used for readability in TOML.
  • pytest-cov supports xdist natively, so pytest --cov still produces one combined report and enforces the 80% threshold.
  • Debugging that needs a single process (--pdb, -s with interactive input) works by overriding on the command line: pytest -n 0.

Changes

  • template/pyproject.toml: pytest-xdist in the dev group (list reformatted one-per-line as it grew); --numprocesses=auto in addopts.
  • template/uv.lock: regenerated.

Impact

  • User-facing: pytest output now starts with the worker count line and test order is no longer deterministic across files. Tests that share mutable global state must be made independent or grouped with @pytest.mark.xdist_group.
  • Tiny suites (like the template's) become slightly slower because workers are spawned (about 0.5s here); real suites get faster.
  • Projects updating from the template receive the config change but keep their own uv.lock (feat: keep uv.lock across template updates and print next steps #27); they need uv lock to add pytest-xdist.
  • CI workflows are unchanged.

Validation Results

# Python 3.10 project rendered from this branch
uv sync --locked
uv run --frozen pytest --cov
# created: 10/10 workers
# 10 workers [3 items]
# TOTAL   5   0   0   0   100%
# Required test coverage of 80.0% reached. Total coverage: 100.00%
# 3 passed in 0.46s

# Python 3.14 project generated with copier copy from HEAD
uv sync --locked
uv run --frozen ruff format . --check && uv run --frozen ruff check . && uv run --frozen ty check
uv run --frozen pytest --cov      # 3 passed, coverage 100% with workers
uv run --frozen pytest -n 0 -q    # 3 passed (single-process override)

- Add pytest-xdist to the dev group and regenerate uv.lock
- Pass --numprocesses=auto through [tool.pytest] addopts so every pytest
  invocation, local or CI, distributes tests across all CPU cores
@mjun0812 mjun0812 added the enhancement New feature or request label Sep 7, 2026
@mjun0812 mjun0812 self-assigned this Sep 7, 2026
@mjun0812
mjun0812 merged commit eb39ae7 into main Sep 7, 2026
7 checks passed
@mjun0812
mjun0812 deleted the feat/pytest-xdist branch September 7, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant