/hep-python-tools:pytest-speedupfrom hep-python-tools
Profiles and accelerates slow pytest suites: measures test durations, reduces collection time, parallelizes with xdist/split, blocks accidental network access, and optimizes CI run time.
Ships in hep-python-tools. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
A slow test suite discourages running tests locally and wastes CI minutes. The discipline is measure-first: change one thing, measure locally and on CI, keep it only if it actually helps. HEP suites have specific bottlenecks — heavy import cost from libraries like uproot, awkward, and torch shows up on every collection even for a single test; large ROOT files inflate fixture setup; and network-backed fixtures accidentally call xrootd or rucio endpoints. Address the bottleneck, not the symptom.
pytest run feels slow and you want to identify where time is spenthep-python-tools:python-testinghep-python-tools:code-quality-tools| Concept | Notes |
|---|---|
pytest --durations N | Print N slowest tests/setup/teardown after a run |
pytest --collect-only | Measure collection time without running any tests |
pytest --noconftest | Skip conftest.py loading — isolates conftest import cost |
pytest --trace-config | List all active plugins including builtins |
pytest --lf | Re-run only last-failed tests (fast local iteration) |
PYTHONDONTWRITEBYTECODE | Skip .pyc generation — small win on dev machines and CI |
pytest-xdist | Parallel execution across CPU cores; -n auto uses all cores |
pytest-split | CI sharding: splits by stored durations across N runners |
pytest-skip-slow | Mark tests @pytest.mark.slow; skip unless --slow passed |
pytest-socket | Block socket calls; catches accidental network access |
Measure the whole suite with hyperfine:
# Install: pixi add --feature test hyperfine (or: brew install hyperfine)
hyperfine --warmup 1 'pytest tests/'
Find the ten slowest tests:
pytest --durations 10
Profile import cost (critical for HEP libraries):
python -X importtime -m pytest --collect-only 2>&1 | grep 'import time'
# Paste stderr into: https://kmichel.github.io/python-importtime-graph/
Import cost from uproot, awkward, hist, or torch can dominate collection
time even when running a single test. Defer heavy top-level imports to inside
the fixture body or test function where possible.
Speed up collection in pyproject.toml:
[tool.pytest.ini_options]
testpaths = ["tests"]
norecursedirs = ["docs", "*.egg-info", ".git", ".tox", "notebooks"]
Check whether conftest.py is the bottleneck:
pytest --collect-only --noconftest # significantly faster? conftest is the culprit
Disable .pyc generation in CI env and ~/.profile:
export PYTHONDONTWRITEBYTECODE=1
Disable unused builtin plugins:
# List all active builtins:
pytest --trace-config | grep 'active plugin'
# Disable ones you don't use (saves their import/registration cost):
pytest -p no:doctest -p no:pastebin
Only disable plugins that actually appear in --trace-config; -p no:<name>
errors if no such plugin is registered. (The nose compatibility plugin was
removed in modern pytest, so -p no:nose no longer applies.)
Add permanently via pyproject.toml:
[tool.pytest.ini_options]
addopts = ["-p", "no:doctest", "-p", "no:pastebin"]
Run only last-failed tests (fast local iteration):
pytest --lf # last-failed only
pytest --lf --ff # last-failed first, then the rest
Skip known-slow tests locally:
import pytest
@pytest.mark.slow
def test_full_analysis_chain():
... # opens real ROOT files, hits rucio, takes >10s
pytest # skips @pytest.mark.slow (fast local run)
pytest --slow # runs everything (CI)
Requires pytest-skip-slow. Register the mark to avoid warnings:
[tool.pytest.ini_options]
markers = ["slow: marks tests as slow (skipped by default; run with --slow)"]
Block inadvertent network access (pytest-socket):
# pyproject.toml — disable globally
[tool.pytest.ini_options]
addopts = ["--disable-socket"]
# conftest.py — allowlist specific hosts for integration tests
import pytest
@pytest.mark.allow_hosts(["xcache.example.cern.ch"])
def test_xrootd_read():
...
Catches accidental xrootd, rucio, or HTTP calls in unit tests that should be fast and offline.
Use tmp_path and scikit-hep-testdata instead of committing large files:
import skhep_testdata
def test_read_nanoaod(tmp_path):
path = skhep_testdata.data_path("nanoAOD_2015_CMS_Open_Data_ttbar.root")
# path is a real local file; use tmp_path for any output artifacts
Avoids multi-MB ROOT blobs in the repository and keeps fixture cost predictable.
Parallelize across cores with pytest-xdist:
pytest -n auto # use all available cores
pytest -n 4 # use exactly 4 workers
Shard across CI runners with pytest-split:
# .github/workflows/ci.yml
strategy:
matrix:
group: [1, 2, 3, 4]
steps:
- run: |
pytest --splits 4 --group ${{ matrix.group }} \
--durations-path .test_durations
Merge coverage after all shards complete:
coverage combine
coverage xml
hyperfine run before and after each change is sufficient.-n auto is not free: worker startup and per-worker session fixture cost
add overhead. On small suites (<100 fast tests), xdist can be slower than a
single-process run.worker_id fixture, or use pytest-split for
CI sharding (which avoids the problem entirely).pytest-split requires a stored durations file (.test_durations): the
first run splits naïvely by count; durations stabilize after a few CI runs.
Also fix the pytest-randomly seed identically across all shards so each
shard gets a consistent subset.pytest tests/unit/test_one.py triggers a full import uproot if it appears
at the top of a conftest.py. Move such imports to fixture bodies or test
functions where possible.pyfakefs does not intercept C-extension I/O: uproot and ROOT open files
via C libraries that bypass the fake filesystem. Use tmp_path for those
tests instead of pyfakefs.filterwarnings = ["error"] plus new plugins: some speedup plugins emit
deprecation warnings under strict settings — audit for new failures after
adding any plugin.pixi add --feature test pytest-xdist pytest-socket pytest-skip-slow) and
run via pixi run pytestPYTHONDONTWRITEBYTECODE=1 in
the workflow env: block; use a matrix strategy for pytest-split shardingpytest-split, always run coverage combine after
all shards before uploading to Codecov or generating the final report--lf: https://docs.pytest.org/en/stable/how-to/cache.htmlWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub usatlas/marketplace --plugin hep-python-tools6 skills in this plugin
/cli-creatorUse when building a Python command-line interface with Typer: adding arguments and options to a script, creating subcommands, wiring an entry point into pyproject.toml, validating CLI inputs with callbacks, or adding a CLI layer to an existing analysis script or HEP workflow./code-quality-toolsUse when setting up code quality tooling for a HEP Python project: configuring pre-commit hooks, adding ruff for linting and auto-formatting, setting up mypy for static type checking, or debugging linter failures in CI or a pre-commit run./python-packagingUse when creating a new Python package, adding pyproject.toml to an existing project, choosing a build backend, setting up src layout, configuring VCS-based versioning, or managing a development environment with pixi or uv for a HEP Python project.