Files
EOS/tests/test_servermarker.py
T
57905b2024 ci: run the tests in parallel jobs and speed up the Sphinx build
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)
2026-11-20 07:15:33 +01:00

91 lines
4.0 KiB
Python

"""The tests that start a real server are marked for selection and for pytest-xdist."""
from typing import Any, Optional, Union
import conftest
import pytest
class FakeItem:
"""Minimal stand-in for a collected test item."""
def __init__(self, *fixturenames: str, marks: tuple[str, ...] = ()) -> None:
self.fixturenames = fixturenames
self.marks: dict[str, tuple[Any, ...]] = {name: () for name in marks}
def get_closest_marker(self, name: str) -> Optional[tuple[Any, ...]]:
"""Return the arguments of the marker, or None if the item does not carry it."""
return self.marks.get(name)
def add_marker(self, marker: Union[str, pytest.MarkDecorator]) -> None:
"""Record the marker by its name, as pytest accepts a name or a decorator."""
decorator = getattr(pytest.mark, marker) if isinstance(marker, str) else marker
self.marks[decorator.name] = decorator.args
def marks_after_marking(
*fixturenames: str, marks: tuple[str, ...] = ()
) -> dict[str, tuple[Any, ...]]:
"""Run the server marking for one item and return the markers it carries then."""
item = FakeItem(*fixturenames, marks=marks)
conftest.mark_server_tests([item]) # type: ignore[list-item]
return item.marks
class TestServerMarker:
"""Marking of the tests that start an EOS or EOSdash server."""
def test_test_using_xprocess_is_marked_and_grouped(self) -> None:
"""A test that uses the xprocess fixture starts a server on the fixed ports."""
assert marks_after_marking("config_eos", "xprocess") == {
"eos_server": (),
"xdist_group": ("eos_server",),
}
def test_explicitly_marked_test_is_grouped(self) -> None:
"""A test that binds the ports without a fixture carries the marker itself."""
assert marks_after_marking("config_eos", marks=("eos_server",)) == {
"eos_server": (),
"xdist_group": ("eos_server",),
}
def test_other_tests_are_left_alone(self) -> None:
assert marks_after_marking("config_eos", "tmp_path") == {}
def test_marking_runs_before_other_collection_hooks(self, request: pytest.FixtureRequest) -> None:
"""pytest-xdist reads the group in its own hook, which must run after the marking.
pluggy calls the plain hook implementations from the end of this list. The marking
and everything behind it must be tryfirst, so that no ordinary implementation, such
as the one of pytest-xdist on a worker, runs before it. Wrappers are left out: they
surround all plain implementations whatever their order.
"""
hook = request.config.pluginmanager.hook.pytest_collection_modifyitems
implementations = [
implementation
for implementation in hook.get_hookimpls()
if not (implementation.hookwrapper or getattr(implementation, "wrapper", False))
]
marking = [
implementation
for implementation in implementations
if implementation.function is conftest.pytest_collection_modifyitems
]
assert len(marking) == 1
called_first = implementations[implementations.index(marking[0]) :]
assert all(implementation.tryfirst for implementation in called_first)
def test_no_unmarked_server_test_is_selected(self, request: pytest.FixtureRequest) -> None:
"""Every selected test of this session that uses xprocess carries both markers.
A plain run checks all server tests this way. In a CI shard the session holds only
the modules of that shard, and with working marking none of their server tests is
selected; the test-count job checks the marking for the CI split.
"""
for item in request.session.items:
if "xprocess" in getattr(item, "fixturenames", ()):
assert item.get_closest_marker("eos_server") is not None, item.nodeid
group = item.get_closest_marker("xdist_group")
assert group is not None and group.args == ("eos_server",), item.nodeid