All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 Allows configuring optional test markers in config, see pyproject.toml.
4 Run optional tests with `pytest --run-optional=...`.
6 Mark tests to run only if an optional test ISN'T selected by prepending the mark with
9 You can specify a "no_" prefix straight in config, in which case you can mark tests
10 to run when this tests ISN'T selected by omitting the "no_" prefix.
12 Specifying the name of the default behavior in `--run-optional=` is harmless.
14 Adapted from https://pypi.org/project/pytest-optional-tests/, (c) 2019 Reece Hart
17 from functools import lru_cache
21 from typing import FrozenSet, List, Set, TYPE_CHECKING
24 from _pytest.store import StoreKey
26 log = logging.getLogger(__name__)
30 from _pytest.config.argparsing import Parser
31 from _pytest.config import Config
32 from _pytest.mark.structures import MarkDecorator
33 from _pytest.nodes import Node
36 ALL_POSSIBLE_OPTIONAL_MARKERS = StoreKey[FrozenSet[str]]()
37 ENABLED_OPTIONAL_MARKERS = StoreKey[FrozenSet[str]]()
40 def pytest_addoption(parser: "Parser") -> None:
41 group = parser.getgroup("collect")
47 help="Optional test markers to run; comma-separated",
49 parser.addini("optional-tests", "List of optional tests markers", "linelist")
52 def pytest_configure(config: "Config") -> None:
53 """Optional tests are markers.
55 Use the syntax in https://docs.pytest.org/en/stable/mark.html#registering-marks.
57 ot_ini = config.inicfg.get("optional-tests") or []
59 ot_run: Set[str] = set()
60 if isinstance(ot_ini, str):
61 ot_ini = ot_ini.strip().split("\n")
62 marker_re = re.compile(r"^\s*(?P<no>no_)?(?P<marker>\w+)(:\s*(?P<description>.*))?")
64 m = marker_re.match(ot)
66 raise ValueError(f"{ot!r} doesn't match pytest marker syntax")
68 marker = (m.group("no") or "") + m.group("marker")
69 description = m.group("description")
70 config.addinivalue_line("markers", f"{marker}: {description}")
71 config.addinivalue_line(
72 "markers", f"{no(marker)}: run when `{marker}` not passed"
74 ot_markers.add(marker)
76 # collect requested optional tests
77 passed_args = config.getoption("run_optional")
79 ot_run.update(itertools.chain.from_iterable(a.split(",") for a in passed_args))
80 ot_run |= {no(excluded) for excluded in ot_markers - ot_run}
81 ot_markers |= {no(m) for m in ot_markers}
83 log.info("optional tests to run:", ot_run)
84 unknown_tests = ot_run - ot_markers
86 raise ValueError(f"Unknown optional tests wanted: {unknown_tests!r}")
89 store[ALL_POSSIBLE_OPTIONAL_MARKERS] = frozenset(ot_markers)
90 store[ENABLED_OPTIONAL_MARKERS] = frozenset(ot_run)
93 def pytest_collection_modifyitems(config: "Config", items: "List[Node]") -> None:
95 all_possible_optional_markers = store[ALL_POSSIBLE_OPTIONAL_MARKERS]
96 enabled_optional_markers = store[ENABLED_OPTIONAL_MARKERS]
99 all_markers_on_test = {m.name for m in item.iter_markers()}
100 optional_markers_on_test = all_markers_on_test & all_possible_optional_markers
101 if not optional_markers_on_test or (
102 optional_markers_on_test & enabled_optional_markers
105 log.info("skipping non-requested optional", item)
106 item.add_marker(skip_mark(frozenset(optional_markers_on_test)))
110 def skip_mark(tests: FrozenSet[str]) -> "MarkDecorator":
111 names = ", ".join(sorted(tests))
112 return pytest.mark.skip(reason=f"Marked with disabled optional tests ({names})")
116 def no(name: str) -> str:
117 if name.startswith("no_"):
118 return name[len("no_") :]