Skip to content

Commit 268042d

Browse files
Merge pull request #1547 from VWS-Python/faster-custom
Use asyncio to make custom test much faster
2 parents eb9399b + 3cb3148 commit 268042d

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

ci/custom_linters.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Custom lint tests.
33
"""
44

5-
import subprocess
5+
import asyncio
66
from pathlib import Path
77

88
import pytest
@@ -23,16 +23,20 @@ def _ci_patterns() -> set[str]:
2323
return ci_patterns
2424

2525

26-
def _tests_from_pattern(ci_pattern: str) -> set[str]:
26+
async def _tests_from_pattern(ci_pattern: str) -> set[str]:
2727
"""
2828
From a CI pattern, get all tests ``pytest`` would collect.
2929
"""
3030
tests: set[str] = set()
3131
args = ["pytest", "-q", "--collect-only", ci_pattern]
32-
result = subprocess.run(args=args, stdout=subprocess.PIPE, check=True)
33-
for line in result.stdout.decode().splitlines():
34-
if line and "collected in" not in line:
35-
tests.add(line)
32+
process = await asyncio.create_subprocess_exec(
33+
*args,
34+
stdout=asyncio.subprocess.PIPE,
35+
)
36+
data, _ = await process.communicate()
37+
for line in data.splitlines():
38+
if line and b"collected in" not in line:
39+
tests.add(line.decode())
3640
return tests
3741

3842

@@ -50,16 +54,22 @@ def test_ci_patterns_valid() -> None:
5054
assert collect_only_result == 0, message
5155

5256

53-
def test_tests_collected_once() -> None:
57+
@pytest.mark.asyncio()
58+
async def test_tests_collected_once() -> None:
5459
"""
5560
Each test in the test suite is collected exactly once.
5661
5762
This does not necessarily mean that they are run - they may be skipped.
5863
"""
5964
ci_patterns = _ci_patterns()
6065
tests_to_patterns: dict[str, set[str]] = {}
66+
tasks = []
6167
for pattern in ci_patterns:
62-
tests = _tests_from_pattern(ci_pattern=pattern)
68+
tasks.append(_tests_from_pattern(ci_pattern=pattern))
69+
70+
results = await asyncio.gather(*tasks)
71+
for index, pattern in enumerate(ci_patterns):
72+
tests = results[index]
6373
for test in tests:
6474
if test in tests_to_patterns:
6575
tests_to_patterns[test].add(pattern)
@@ -74,6 +84,6 @@ def test_tests_collected_once() -> None:
7484
)
7585
assert len(patterns) == 1, message
7686

77-
all_tests = _tests_from_pattern(ci_pattern=".")
87+
all_tests = await _tests_from_pattern(ci_pattern=".")
7888
assert tests_to_patterns.keys() - all_tests == set()
7989
assert all_tests - tests_to_patterns.keys() == set()

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ dev = [
296296
"pylint==2.17.2",
297297
"pyright==1.1.301",
298298
"pyroma==4.2",
299+
"pytest-asyncio==0.21.0",
299300
"pytest-cov==4.0.0",
300301
"pytest==7.2.2",
301302
"requests-mock-flask==2023.3.5.1",

0 commit comments

Comments
 (0)