Skip to content

Commit 559ffe2

Browse files
committed
Add parallel testing demo
1 parent e76d2fa commit 559ffe2

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
name: Parallel (file)
2+
3+
on:
4+
push:
5+
schedule:
6+
- cron: "0 0 1 * *" # Midnight every month (UTC)
7+
workflow_dispatch:
8+
9+
jobs:
10+
collect:
11+
name: Collect
12+
runs-on: ubuntu-latest
13+
outputs:
14+
groups: "${{ steps.build.outputs.TEST_GROUPS }}"
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- uses: astral-sh/setup-uv@v6
20+
with:
21+
enable-cache: true
22+
23+
- name: Install python dependencies
24+
run: |
25+
uv python install; echo
26+
uv sync --locked; echo
27+
uv tree
28+
29+
- name: Collect tests
30+
run: |
31+
uv run -- pytest --collect-only --quiet | tee collected.tmp
32+
echo '::group::Delete the last two lines'
33+
sed -e '$d' collected.tmp | sed -e '$d' | tee collected.txt
34+
echo '::endgroup::'
35+
36+
- name: Build test matrix
37+
id: build
38+
shell: uv run -- python {0}
39+
run: |
40+
from os import environ
41+
42+
# Deduplicate file paths. The same file will show up
43+
# multiple times because each file can have many tests.
44+
with open("collected.txt", encoding="utf-8") as lines:
45+
paths = sorted({line.strip().partition("::")[0] for line in lines})
46+
47+
max_size = 3
48+
test_groups = {"include": []}
49+
50+
# Slice the list of paths into subgroups.
51+
for i, j in enumerate(range(0, len(paths), max_size), start=1):
52+
test_groups["include"].append({"name": f"Group {i}", "files": " ".join(paths[j : j + max_size])})
53+
54+
print(f"::notice ::Groups: {test_groups}")
55+
with open(environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f:
56+
print(f"TEST_GROUPS={test_groups}", file=f)
57+
58+
test:
59+
name: Test ${{ matrix.name }}
60+
runs-on: ubuntu-latest
61+
needs: collect
62+
63+
strategy:
64+
matrix: ${{ fromJSON(needs.collect.outputs.groups) }}
65+
66+
steps:
67+
- uses: actions/checkout@v4
68+
69+
- uses: astral-sh/setup-uv@v6
70+
with:
71+
enable-cache: true
72+
73+
- name: Install python dependencies
74+
run: |
75+
uv python install; echo
76+
uv sync --locked; echo
77+
uv tree
78+
79+
- name: Run test
80+
run: uv run -- pytest '${{ matrix.files }}'

0 commit comments

Comments
 (0)