mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-11-25 15:06:20 +01:00
The single test job ran all tests one after the other on one runner,
including the Sphinx build and the Docker checks (11-19 min). It is now
split so that every test still runs exactly once:
- docs-test, docker-test: the Sphinx build (new marker "docs") and the
Docker / Home Assistant add-on checks ("docker") in jobs of their own.
- test-server: the tests that start EOS or EOSdash on the fixed ports.
The collection hook marks every test that uses the xprocess fixture
with "eos_server" and puts them into one xdist group, so
"pytest -n auto --dist loadgroup" also works locally. A test that
binds the ports without a fixture carries the marker itself.
- test-shard: all other tests in four shards of about equal runtime
(scripts/shard_test_files.py, runtimes in scripts/test_durations.json),
each with pytest-xdist.
- test-count: fails if these parts do not add up to the tests of one
plain run.
- test: keeps the name of the required check. It prints the combined
coverage report and fails if any of the jobs above failed.
COVERAGE_CORE=sysmon measures line coverage through sys.monitoring:
the report is the same, the test time drops by about a third. The
combined report misses 1-3 statements more than a single run, in lines
that only run when a test finds state left by an earlier test.
The Sphinx build spent 74 % of its time in one search of
PythonDomain.find_obj (sphinx-doc/sphinx#11282). The local extension
docs/_ext/pyxrefindex.py answers it from an index of name endings with
the same matches in the same order; a build has a byte-identical
objects.inv. It copies the logic of Sphinx 9.0.4 and is only active for
that version; TestPyXrefIndex compares it with the installed Sphinx and
fails after a Sphinx update until the version is raised. The
documentation test builds with -j (up to four jobs); its pages can
differ from a serial build in the target of ambiguous cross-references.
Read the Docs builds without -j. Sphinx build on GitHub runners:
427-480 s -> 180-233 s.
make test-ci runs the commands of these jobs one after the other.
On GitHub runners the tests are done after about 2 minutes; the
required check ends with docs-test after about 4-5 minutes.
Additional changes
Add pytest-xdist to the development dependencies
List the 25 slowest tests in each test job (--durations=25)
Test version() directly; it was covered only through the Sphinx test
Add a "Keep Tests Fast" tip, the server marker rule, the extension and
test-ci to the development guide and the testing skill
Refs #1553
Co-authored-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: SchaafAlexander <86607129+SchaafAlexander@users.noreply.github.com>
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-fable-5-2)
121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
"""Tests for the script that splits the pytest modules into CI shards."""
|
|
|
|
import importlib.util
|
|
from pathlib import Path
|
|
from types import ModuleType
|
|
|
|
import pytest
|
|
|
|
|
|
def _load_shard_module() -> ModuleType:
|
|
"""Load the shard script directly from its source file.
|
|
|
|
Returns:
|
|
The imported shard script module.
|
|
|
|
Raises:
|
|
ImportError: If the module cannot be loaded.
|
|
"""
|
|
script_path = Path(__file__).resolve().parents[1] / "scripts" / "shard_test_files.py"
|
|
spec = importlib.util.spec_from_file_location("shard_test_files", script_path)
|
|
if spec is None or spec.loader is None:
|
|
raise ImportError(f"Cannot load the shard script from {script_path}")
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
_shard = _load_shard_module()
|
|
|
|
MODULES = [f"tests/test_m{index}.py" for index in range(11)]
|
|
DURATIONS = {"tests/test_m0.py": 60.0, "tests/test_m1.py": 50.0, "tests/test_m2.py": 5.0}
|
|
|
|
|
|
class TestSplit:
|
|
"""Distribution of the test modules to the shards."""
|
|
|
|
@pytest.mark.parametrize(
|
|
("shards", "durations"),
|
|
[
|
|
(1, DURATIONS),
|
|
(2, {}),
|
|
(4, DURATIONS),
|
|
(20, DURATIONS),
|
|
],
|
|
)
|
|
def test_every_module_is_in_exactly_one_shard(
|
|
self, shards: int, durations: dict[str, float]
|
|
) -> None:
|
|
"""No module is dropped or run twice, with or without runtimes."""
|
|
result = _shard.split(MODULES, durations, shards)
|
|
|
|
assert len(result) == shards
|
|
assert sorted(module for shard in result for module in shard) == sorted(MODULES)
|
|
|
|
def test_heavy_modules_go_to_different_shards(self) -> None:
|
|
"""The two modules that dominate the runtime do not share a shard."""
|
|
result = _shard.split(MODULES, DURATIONS, 2)
|
|
|
|
assert ["tests/test_m0.py" in shard for shard in result] == [True, False]
|
|
assert ["tests/test_m1.py" in shard for shard in result] == [False, True]
|
|
|
|
def test_split_does_not_depend_on_the_input_order(self) -> None:
|
|
"""All jobs of a workflow run compute the same distribution."""
|
|
assert _shard.split(MODULES, DURATIONS, 4) == _shard.split(
|
|
list(reversed(MODULES)), DURATIONS, 4
|
|
)
|
|
|
|
def test_non_positive_shard_count_is_rejected(self) -> None:
|
|
with pytest.raises(ValueError, match="must be positive"):
|
|
_shard.split(MODULES, DURATIONS, 0)
|
|
|
|
|
|
class TestModuleList:
|
|
"""The modules the script distributes."""
|
|
|
|
def test_modules_are_the_test_files_directly_in_tests(self) -> None:
|
|
tests_dir = Path(__file__).resolve().parent
|
|
modules = _shard.list_test_modules()
|
|
|
|
assert f"tests/{Path(__file__).name}" in modules
|
|
assert "tests/conftest.py" not in modules
|
|
assert modules == sorted(set(modules))
|
|
for module in modules:
|
|
path = tests_dir / Path(module).name
|
|
assert module == f"tests/{path.name}"
|
|
assert path.name.startswith("test_") and path.is_file()
|
|
|
|
|
|
class TestDurations:
|
|
"""The stored runtimes and their update from junit reports."""
|
|
|
|
def test_missing_file_counts_every_module_alike(self, tmp_path: Path) -> None:
|
|
assert _shard.load_durations(tmp_path / "missing.json") == {}
|
|
|
|
def test_stored_runtimes_are_seconds_by_module_path(self) -> None:
|
|
durations = _shard.load_durations()
|
|
|
|
assert durations
|
|
assert all(
|
|
name.startswith("tests/test_") and seconds >= 0 for name, seconds in durations.items()
|
|
)
|
|
|
|
def test_junit_cases_are_summed_by_module(self, tmp_path: Path) -> None:
|
|
report = tmp_path / "junit.xml"
|
|
report.write_text(
|
|
'<?xml version="1.0" encoding="utf-8"?>'
|
|
'<testsuites><testsuite name="pytest">'
|
|
'<testcase classname="tests.test_alpha.TestThing" name="test_a" time="1.25"/>'
|
|
'<testcase classname="tests.test_alpha" name="test_b" time="0.5"/>'
|
|
'<testcase classname="tests.test_beta.TestOther" name="test_c" time="2.004"/>'
|
|
'<testcase classname="other.package" name="test_d" time="9"/>'
|
|
"</testsuite></testsuites>",
|
|
encoding="utf-8",
|
|
)
|
|
|
|
assert _shard.durations_from_junit([report]) == {
|
|
"tests/test_alpha.py": 1.75,
|
|
"tests/test_beta.py": 2.0,
|
|
}
|