pytest-shard
pytest-shard is a pytest plugin (version 0.1.2) designed to parallelize test execution across multiple machines by sharding tests based on a hash of their test names. It enables fine-grained parallelism at the individual test case level, making it suitable for various continuous integration (CI) services. The library appears to be in a maintenance phase, with the last PyPI update in December 2020.
Warnings
- gotcha pytest-shard does not take into account the run time of tests when sharding, which can lead to suboptimal load balancing and uneven test execution times across shards. This may result in some CI jobs finishing significantly later than others, despite an even numerical distribution of tests.
- gotcha Sharding is based on a hash of the test name. Renaming tests or significantly changing their structure can alter their hash, causing them to move to a different shard. This can lead to unexpected test distribution changes in CI, potentially causing certain tests to be rerun on different machines or requiring manual re-evaluation of shard contents.
- gotcha The `--shard-id` command-line option in pytest-shard is 0-indexed (e.g., `--shard-id=0` for the first shard). Users configuring CI environments should ensure their environment variables (like `CIRCLE_NODE_INDEX` or `TRAVIS_JOB_NUMBER`) are correctly mapped to this 0-indexed scheme to avoid skipping shards or encountering out-of-bounds errors.
Install
-
pip install pytest-shard
Imports
- pytest-shard plugin
No direct Python import needed; it's a pytest plugin automatically discovered.
Quickstart
# Example: Shard tests across two machines
# On machine 1:
pytest --shard-id=0 --num-shards=2
# On machine 2:
pytest --shard-id=1 --num-shards=2
# Example for CircleCI:
pytest --shard-id=${CIRCLE_NODE_INDEX} --num-shards=${CIRCLE_NODE_TOTAL}