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.
pip install pytest-shardVerified import paths — ran on the pinned version, not inferred.
After installation, pytest-shard is enabled by passing `--shard-id` and `--num-shards` arguments to the `pytest` command. `--shard-id` specifies the index of the current shard (0-indexed), and `--num-shards` specifies the total number of shards. This is typically configured in CI pipelines where tests are distributed across parallel jobs.
Manually balance test durations across files/modules if even time distribution is critical. Consider alternative sharding mechanisms or CI-specific tools that track test durations for more sophisticated load balancing.
Be aware that test renames will re-shard tests. If consistent sharding across minor changes is critical, consider using more stable test identifiers or a different sharding strategy. Avoid frequent, large-scale renames without understanding the impact on sharding.
Ensure CI environment variables providing the shard index are 0-based. If a variable is 1-based (e.g., `TRAVIS_JOB_NUMBER`), adjust it (e.g., `--shard-id=$((${TRAVIS_JOB_NUMBER} - 1))`) when passing to pytest.