Skip to content

Commit 9b45922

Browse files
authored
use pytest-xdist to utilize multiple workers for Python tests (danmar#6696)
1 parent 3a0f039 commit 9b45922

File tree

8 files changed

+88
-74
lines changed

8 files changed

+88
-74
lines changed

.github/workflows/CI-unixish.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ jobs:
268268
strategy:
269269
matrix:
270270
os: [ubuntu-20.04, ubuntu-22.04, macos-13]
271+
include:
272+
- xdist_n: auto
273+
# FIXME: test_color_tty fails with xdist
274+
- os: macos-13
275+
xdist_n: '1'
271276
fail-fast: false # Prefer quick result
272277

273278
runs-on: ${{ matrix.os }}
@@ -306,6 +311,7 @@ jobs:
306311
python3 -m pip install pip --upgrade
307312
python3 -m pip install pytest
308313
python3 -m pip install pytest-timeout
314+
python3 -m pip install pytest-xdist
309315
python3 -m pip install psutil
310316
311317
- name: Build cppcheck
@@ -329,7 +335,7 @@ jobs:
329335
330336
- name: Run test/cli
331337
run: |
332-
python3 -m pytest -Werror --strict-markers -vv test/cli
338+
python3 -m pytest -Werror --strict-markers -vv -n ${{ matrix.xdist_n }} test/cli
333339
334340
# TODO: use the step below instead
335341
# do not use pushd in this step since we go below the working directory
@@ -338,26 +344,26 @@ jobs:
338344
cd ..
339345
ln -s cppcheck 'cpp check'
340346
cd 'cpp check/test/cli'
341-
python3 -m pytest -Werror --strict-markers -vv
347+
python3 -m pytest -Werror --strict-markers -vv -n ${{ matrix.xdist_n }}
342348
343349
# FIXME: proj2_test.py fails because of the relative path cleanups in ImportProject::setRelativePaths()
344350
# It fails because the application path used as base path has its symlink resolved by getcwd().
345351
- name: Run test/cli (symlink)
346352
if: false
347353
run: |
348354
ln -s . 'cpp check'
349-
python3 -m pytest -Werror --strict-markers -vv 'cpp check/test/cli'
355+
python3 -m pytest -Werror --strict-markers -vv -n ${{ matrix.xdist_n }} 'cpp check/test/cli'
350356
351357
- name: Run test/cli (-j2)
352358
run: |
353-
python3 -m pytest -Werror --strict-markers -vv test/cli
359+
python3 -m pytest -Werror --strict-markers -vv -n ${{ matrix.xdist_n }} test/cli
354360
env:
355361
TEST_CPPCHECK_INJECT_J: 2
356362

357363
- name: Run test/cli (--clang)
358364
if: false
359365
run: |
360-
python3 -m pytest -Werror --strict-markers -vv test/cli
366+
python3 -m pytest -Werror --strict-markers -vv -n ${{ matrix.xdist_n }} test/cli
361367
env:
362368
TEST_CPPCHECK_INJECT_CLANG: clang
363369

.github/workflows/CI-windows.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ jobs:
143143
python -m pip install pytest || exit /b !errorlevel!
144144
python -m pip install pytest-custom_exit_code || exit /b !errorlevel!
145145
python -m pip install pytest-timeout || exit /b !errorlevel!
146+
python -m pip install pytest-xdist || exit /b !errorlevel!
146147
python -m pip install psutil || exit /b !errorlevel!
147148
148149
# TODO: build with CMake
@@ -182,20 +183,20 @@ jobs:
182183
- name: Run test/cli
183184
if: matrix.config == 'release'
184185
run: |
185-
python -m pytest -Werror --strict-markers -vv test/cli || exit /b !errorlevel!
186+
python -m pytest -Werror --strict-markers -vv -n auto test/cli || exit /b !errorlevel!
186187
187188
- name: Run test/cli (-j2)
188189
if: matrix.config == 'release'
189190
run: |
190-
python -m pytest -Werror --strict-markers -vv test/cli || exit /b !errorlevel!
191+
python -m pytest -Werror --strict-markers -vv -n auto test/cli || exit /b !errorlevel!
191192
env:
192193
TEST_CPPCHECK_INJECT_J: 2
193194

194195
# TODO: install clang
195196
- name: Run test/cli (--clang)
196197
if: false # matrix.config == 'release'
197198
run: |
198-
python -m pytest -Werror --strict-markers -vv test/cli || exit /b !errorlevel!
199+
python -m pytest -Werror --strict-markers -vv -n auto test/cli || exit /b !errorlevel!
199200
env:
200201
TEST_CPPCHECK_INJECT_CLANG: clang
201202

.github/workflows/asan.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
python3 -m pip install pip --upgrade
6868
python3 -m pip install pytest
6969
python3 -m pip install pytest-timeout
70+
python3 -m pip install pytest-xdist
7071
python3 -m pip install psutil
7172
7273
# TODO: disable all warnings
@@ -103,20 +104,20 @@ jobs:
103104
- name: Run test/cli
104105
run: |
105106
pwd=$(pwd)
106-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
107+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
107108
108109
- name: Run test/cli (-j2)
109110
run: |
110111
pwd=$(pwd)
111-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
112+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
112113
env:
113114
TEST_CPPCHECK_INJECT_J: 2
114115

115116
- name: Run test/cli (--clang)
116117
if: false
117118
run: |
118119
pwd=$(pwd)
119-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
120+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
120121
env:
121122
TEST_CPPCHECK_INJECT_CLANG: clang
122123

.github/workflows/scriptcheck.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ jobs:
9292
python -m pip install 'pylint<=3.3.0'
9393
python -m pip install unittest2
9494
python -m pip install pytest
95+
python -m pip install pytest-xdist
9596
python -m pip install pygments
9697
python -m pip install requests
9798
python -m pip install psutil
@@ -135,18 +136,20 @@ jobs:
135136
run: |
136137
python tools/test_matchcompiler.py
137138
139+
# TODO: run with "-n auto" when misra_test.py can be run in parallel
138140
# we cannot specify -Werror since xml/etree/ElementTree.py in Python 3.9/3.10 contains an unclosed file
139141
- name: test addons
140142
if: matrix.python-version == '3.9' || matrix.python-version == '3.10'
141143
run: |
142-
python -m pytest --strict-markers -vv addons/test
144+
python -m pytest --strict-markers -vv -n 1 addons/test
143145
env:
144146
PYTHONPATH: ./addons
145147

148+
# TODO: run with "-n auto" when misra_test.py can be run in parallel
146149
- name: test addons
147150
if: matrix.python-version != '3.9' && matrix.python-version != '3.10'
148151
run: |
149-
python -m pytest -Werror --strict-markers -vv addons/test
152+
python -m pytest -Werror --strict-markers -vv -n 1 addons/test
150153
env:
151154
PYTHONPATH: ./addons
152155

.github/workflows/tsan.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
python3 -m pip install pip --upgrade
6868
python3 -m pip install pytest
6969
python3 -m pip install pytest-timeout
70+
python3 -m pip install pytest-xdist
7071
python3 -m pip install psutil
7172
7273
- name: CMake
@@ -103,22 +104,22 @@ jobs:
103104
- name: Run test/cli
104105
run: |
105106
pwd=$(pwd)
106-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
107+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
107108
env:
108109
TEST_CPPCHECK_INJECT_EXECUTOR: thread
109110

110111
- name: Run test/cli (-j2)
111112
run: |
112113
pwd=$(pwd)
113-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
114+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
114115
env:
115116
TEST_CPPCHECK_INJECT_J: 2
116117

117118
- name: Run test/cli (--clang)
118119
if: false
119120
run: |
120121
pwd=$(pwd)
121-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
122+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
122123
env:
123124
TEST_CPPCHECK_INJECT_CLANG: clang
124125

.github/workflows/ubsan.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ jobs:
6767
python3 -m pip install pip --upgrade
6868
python3 -m pip install pytest
6969
python3 -m pip install pytest-timeout
70+
python3 -m pip install pytest-xdist
7071
python3 -m pip install psutil
7172
7273
# TODO: disable warnings
@@ -103,20 +104,20 @@ jobs:
103104
- name: Run test/cli
104105
run: |
105106
pwd=$(pwd)
106-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
107+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
107108
108109
- name: Run test/cli (-j2)
109110
run: |
110111
pwd=$(pwd)
111-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
112+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
112113
env:
113114
TEST_CPPCHECK_INJECT_J: 2
114115

115116
- name: Run test/cli (--clang)
116117
if: false
117118
run: |
118119
pwd=$(pwd)
119-
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
120+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv -n auto test/cli
120121
env:
121122
TEST_CPPCHECK_INJECT_CLANG: clang
122123

addons/test/misra_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def checker():
3030
return MisraChecker(settings)
3131

3232

33+
# FIXME: files are generates in the source tree so it will cause issues if tests are run with xdist.
3334
@pytest.fixture
3435
def test_files():
3536
for f in TEST_SOURCE_FILES:

0 commit comments

Comments
 (0)