Skip to content

Commit 0fd7db5

Browse files
committed
use pytest-split in GHA test workflow
1 parent 3766783 commit 0fd7db5

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
from pathlib import Path
3+
4+
split_prefix = "split-"
5+
durations_path = Path(".test_durations")
6+
7+
split_paths = Path(".").glob(f"{split_prefix}*/{durations_path.name}")
8+
9+
try:
10+
previous_durations = json.loads(durations_path.read_text())
11+
except FileNotFoundError:
12+
previous_durations = {}
13+
14+
new_durations = previous_durations.copy()
15+
16+
for path in split_paths:
17+
durations = json.loads(path.read_text())
18+
new_durations.update(
19+
{
20+
key: duration
21+
for key, duration in durations.items()
22+
if previous_durations[key] != duration
23+
}
24+
)
25+
26+
durations_path.parent.mkdir(parents=True, exist_ok=True)
27+
durations_path.write_text(json.dumps(new_durations))

.github/workflows/test.yml

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ jobs:
1010
test:
1111
if: github.repository_owner == 'hackingmaterials' # don't run on forks
1212
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
# pytest-split groups for automatically equally split concurrent test runners
16+
group: [1, 2, 3, 4, 5]
1317
services:
1418
mongodb:
1519
image: mongo
@@ -45,6 +49,49 @@ jobs:
4549
pip install -r requirements-ci.txt
4650
pip install .[complete]
4751
48-
- name: pytest
52+
- name: Get durations from cache
53+
uses: actions/cache@v2
54+
with:
55+
path: test_durations
56+
# the key must never match, even when restarting workflows, as that
57+
# will cause durations to get out of sync between groups, the
58+
# combined durations will be loaded if available
59+
key: test-durations-split-${{ github.run_id }}-${{ github.run_number}}-${{ matrix.group }}
60+
restore-keys: |
61+
test-durations-combined-${{ github.sha }}
62+
test-durations-combined
63+
64+
- name: Run tests
4965
run: |
50-
pytest --ignore=atomate/qchem/test_files --cov=atomate --cov-report html:coverage_reports atomate
66+
pytest --splits 5 --group ${{ matrix.group }} --store-durations \
67+
--cov=atomate --cov-report html:coverage_reports atomate
68+
69+
- name: Upload partial durations
70+
uses: actions/upload-artifact@v2
71+
with:
72+
name: split-${{ matrix.group }}
73+
path: .test_durations
74+
75+
update_durations:
76+
name: Combine and update test durations
77+
runs-on: ubuntu-latest
78+
needs: test
79+
steps:
80+
- name: Checkout repo
81+
uses: actions/checkout@v2
82+
83+
- name: Get durations from cache
84+
uses: actions/cache@v2
85+
with:
86+
path: .test_durations
87+
# key won't match during the first run for the given commit, but
88+
# restore-key will if there's a previous stored durations file,
89+
# so cache will both be loaded and stored
90+
key: test-durations-combined-${{ github.sha }}
91+
restore-keys: test-durations-combined
92+
93+
- name: Download artifacts
94+
uses: actions/download-artifact@v2
95+
96+
- name: Combine test durations
97+
run: python3 .github/workflows/combine_durations.py

requirements-ci.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ coverage==5.5
33
moto==2.0.10
44
pytest-cov==2.12.1
55
pytest==6.2.4
6+
pytest-split

0 commit comments

Comments
 (0)