How to Split pytest by Timings With pytest-split
pytest-split partitions tests by recorded runtime, not file count, so every group finishes at about the same time.
Record durations once with --store-durations, commit the file, then split with --splits N --group i so slow tests spread evenly.
Steps
- Run
pytest --store-durationsto write.test_durations. - Commit that file so CI can read historical timings.
- Split each job with
--splits N --group ${{ matrix.group }} --durations-path .test_durations.
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
group: [1, 2, 3]
steps:
- uses: actions/checkout@v4
- run: pip install pytest pytest-split
- run: pytest --splits 3 --group ${{ matrix.group }} --durations-path .test_durationsGotchas
- A stale durations file skews balance; refresh it when the suite changes a lot.
- New tests with no recorded timing default to an estimate and may land unevenly.
Related guides
How to Run pytest in Parallel With pytest-xdistSpeed up a Python suite on a single CI runner with pytest-xdist, using -n auto to spread tests across every a…
How to Balance Test Shards Evenly in CIKeep every CI test shard finishing at the same time by splitting on recorded duration instead of file count,…