-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathconftest.py
More file actions
48 lines (38 loc) · 2.01 KB
/
Copy pathconftest.py
File metadata and controls
48 lines (38 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Root-level pytest configuration.
Responsibilities:
1. Ensure the ``--basetemp`` parent directory exists before pytest's
``tmp_path_factory`` tries to create it (avoids FileNotFoundError on
clean CI checkouts with no ``tmp/`` directory present).
2. Auto-apply directory-based markers so tests in ``tests/e2e/`` and
``tests/smoke/`` are always tagged correctly even if a test author
forgets the ``pytestmark`` declaration.
The ``addopts`` and ``markers`` settings live in ``pyproject.toml`` so that
``pytest --help`` surfaces them correctly. See docs/E2E_TESTING.md for the
full marker reference and cost-tier run commands.
"""
from __future__ import annotations
import pathlib
import pytest
def pytest_configure(config) -> None: # noqa: ANN001 — pytest config object
basetemp = config.getoption("basetemp", default=None)
if basetemp:
parent = pathlib.Path(basetemp).expanduser().resolve().parent
parent.mkdir(parents=True, exist_ok=True)
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
"""Auto-apply markers based on the test file's directory.
Tests under ``tests/e2e/`` receive ``pytest.mark.e2e`` if they don't
already carry it. Tests under ``tests/smoke/`` receive ``pytest.mark.smoke``.
This is a safety net — explicit ``pytestmark`` declarations in each file
are still preferred and will be the canonical record.
"""
for item in items:
parts = item.path.parts
# Adjacent-pair check: matches `.../tests/e2e/...` or `.../tests/smoke/...`
# at any nesting depth. Prevents the false-positive a substring check
# would have on a hypothetical `tests/api/e2e_helpers/test_x.py`.
adjacent = set(zip(parts, parts[1:]))
marker_names = {m.name for m in item.iter_markers()}
if ("tests", "e2e") in adjacent and "e2e" not in marker_names:
item.add_marker(pytest.mark.e2e)
if ("tests", "smoke") in adjacent and "smoke" not in marker_names:
item.add_marker(pytest.mark.smoke)