Shard tests to support parallelism across multiple machines.
Project description
繁體中文 | English
pytest-shard
This is a fork of AdamGleave/pytest-shard by Cloud Chen. Modifications include: Allure report integration, multi-shard result merging, nox-based tooling, and modernized packaging with
pyproject.toml.
pytest-shard splits your test suite across multiple machines or CI workers at individual test-case granularity. By default it sorts tests by node ID and assigns them round-robin across shards, so parallelism works even when all tests live in a single file or a single parameterized method.
How It Works
See Sharding Modes for separate diagrams that explain how roundrobin, hash, and duration assign tests to shards.
What it does
| Capability | Description |
|---|---|
| Round-robin sharding (default) | Sorts tests by node ID and interleaves across shards, guaranteeing shard counts differ by at most 1 |
| Hash-based sharding | Assigns each test deterministically via SHA-256(node_id) % N — per-test stable even as the suite grows |
| Duration-based sharding | Greedy bin-packing using a .test_durations file (compatible with pytest-split) to minimise the longest shard |
| Zero configuration | Just add --shard-id and --num-shards — no config files, no test ordering required |
| Any granularity | Splits at the individual test level, not at the file or class level |
| CI-agnostic | Works with GitHub Actions, CircleCI, Travis CI, or any system that runs parallel jobs |
| Allure integration | Collect results per shard, merge them, and view a unified Timeline report |
Documentation
| Guide | Description |
|---|---|
| Sharding Modes | Detailed behavior of roundrobin, hash, and duration, including .test_durations, verbose shard reports, and mode selection guidance. |
| Demo Sessions | How contributors can run the bundled demo suites locally with nox. |
| Allure Report Integration | How to collect Allure results across shards, merge them into one report, run shards in parallel locally, and integrate with GitHub Actions / CircleCI. Includes a worked example with 30 tests across 3 parallel shards and a Timeline screenshot. |
Quick start
Installation
pip install pytest-shard-cloudc
Split tests across N machines
# Machine 0
pytest --shard-id=0 --num-shards=3
# Machine 1
pytest --shard-id=1 --num-shards=3
# Machine 2
pytest --shard-id=2 --num-shards=3
Each machine runs roughly 1/N of the test suite. Together they cover 100% of tests.
Choose a sharding mode
# Round-robin (default) — guaranteed count balance
pytest --shard-id=0 --num-shards=3 --shard-mode=roundrobin
# Hash — per-test stable assignment, stateless
pytest --shard-id=0 --num-shards=3 --shard-mode=hash
# Duration — bin-packing by recorded test times, minimises longest shard
pytest --shard-id=0 --num-shards=3 --shard-mode=duration --durations-path=.test_durations
See Sharding Modes for detailed trade-offs, .test_durations generation, and mode selection guidance.
GitHub Actions example
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
shard_id: [0, 1, 2]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with: { python-version: "3.11" }
- run: pip install pytest-shard-cloudc
- run: pytest --shard-id=${{ matrix.shard_id }} --num-shards=3
CircleCI example
jobs:
test:
parallelism: 3
steps:
- run: pytest --shard-id=${CIRCLE_NODE_INDEX} --num-shards=${CIRCLE_NODE_TOTAL}
More guides
- For mode behavior,
.test_durations, verbose shard reports, and selection strategy, see Sharding Modes. - For contributor-facing demo commands, see Demo Sessions.
- For report generation and parallel execution screenshots, see Allure Report Integration.
Alternatives
pytest-xdist parallelizes tests across CPU cores on a single machine and supports remote workers. A common pattern is to combine both: use pytest-shard to split work across CI nodes, and pytest-xdist to parallelize within each node.
pytest-circleci-parallelized splits by test run time rather than test count, but only at class granularity and only for CircleCI.
Contributions
Contributions are welcome. The package requires Python 3.11 or newer. Install the development toolchain and run the full check suite:
pip install -e .[dev]
nox
The Allure integration test also requires the allure CLI to be available on PATH.
License
MIT licensed.
Original work Copyright 2019 Adam Gleave. Modifications Copyright 2026 Cloud Chen.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_shard_cloudc-0.1.2.tar.gz.
File metadata
- Download URL: pytest_shard_cloudc-0.1.2.tar.gz
- Upload date:
- Size: 287.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5fb20c998bf9ae3ac3fbc5866e3382bc3d35f5de46489be8179aa4abe67fc72
|
|
| MD5 |
a770c5efd81d07c654df61473141f9ab
|
|
| BLAKE2b-256 |
d8e4c7c6a6699520cf30bb59f40130e57ea4bff1bbac666f0f7f6b2e8e2d5ea9
|
Provenance
The following attestation bundles were made for pytest_shard_cloudc-0.1.2.tar.gz:
Publisher:
publish.yml on wolke1007/pytest-shard-cloudc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_shard_cloudc-0.1.2.tar.gz -
Subject digest:
e5fb20c998bf9ae3ac3fbc5866e3382bc3d35f5de46489be8179aa4abe67fc72 - Sigstore transparency entry: 1191296418
- Sigstore integration time:
-
Permalink:
wolke1007/pytest-shard-cloudc@5e55b32129e550acff98ed9f0c84905a3396d902 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/wolke1007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5e55b32129e550acff98ed9f0c84905a3396d902 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_shard_cloudc-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pytest_shard_cloudc-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab848a37be02bfd81e58bd2adda4216e3a9940d0ee7ad56f728fefefbdf29573
|
|
| MD5 |
c400ebc8da2a83bcac9fd5102612e86a
|
|
| BLAKE2b-256 |
6c9d1a08bfcead04f054012a0b6ede8093fea122abf305952cd0161502a43f36
|
Provenance
The following attestation bundles were made for pytest_shard_cloudc-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on wolke1007/pytest-shard-cloudc
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_shard_cloudc-0.1.2-py3-none-any.whl -
Subject digest:
ab848a37be02bfd81e58bd2adda4216e3a9940d0ee7ad56f728fefefbdf29573 - Sigstore transparency entry: 1191296421
- Sigstore integration time:
-
Permalink:
wolke1007/pytest-shard-cloudc@5e55b32129e550acff98ed9f0c84905a3396d902 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/wolke1007
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5e55b32129e550acff98ed9f0c84905a3396d902 -
Trigger Event:
workflow_dispatch
-
Statement type: