Skip to content

Commit f69dca4

Browse files
pytestplugin/hooks: check for broken strategy state in pytest_runtest_setup()
Now that a strategy can be marked broken, check for that before executing a test and its fixtures. If a broken strategy is found in one of the targets, skip the test. It's not worth continuing with it. It only leads to cascading errors. The check for brokenness cannot be performed in the strategy fixture itself because it's session scoped, meaning it only runs for the first test requesting it (directly or indirectly). So wrap pytest's pytest_runtest_setup hook running before fixture/test exexcution, Check if the test requested the strategy fixture (directly or indirectly) and perform the check only in that case. Since we cannot know what target the strategy acts on (the target fixture might be overridden by the test suite), simply check all targets for broken strategies and skip the test if any of these have a broken strategy. This means a strategy exception leads to either a test error (when triggered in a fixture) or a test fail (when triggered in the test itself). All subsequent tests requesting the broken strategy (via the strategy fixture) will be skipped. Signed-off-by: Bastian Krause <[email protected]>
1 parent ea2a464 commit f69dca4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

labgrid/pytestplugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .fixtures import pytest_addoption, env, target, strategy
2-
from .hooks import pytest_configure, pytest_collection_modifyitems, pytest_cmdline_main
2+
from .hooks import pytest_configure, pytest_collection_modifyitems, pytest_cmdline_main, pytest_runtest_setup

labgrid/pytestplugin/hooks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..consoleloggingreporter import ConsoleLoggingReporter
88
from ..util.helper import processwrapper
99
from ..logging import StepFormatter, StepLogger
10+
from ..exceptions import NoStrategyFoundError
1011

1112
LABGRID_ENV_KEY = pytest.StashKey[Environment]()
1213

@@ -130,3 +131,23 @@ def pytest_collection_modifyitems(config, items):
130131
reason=f'Skipping because features "{missing_feature}" are not supported'
131132
)
132133
item.add_marker(skip)
134+
135+
@pytest.hookimpl(tryfirst=True)
136+
def pytest_runtest_setup(item):
137+
"""
138+
Skip test if one of the targets uses a strategy considered broken.
139+
"""
140+
# Before any fixtures run for the test, check if the session-scoped strategy fixture was
141+
# requested (might have been executed already for a prior test). If that's the case and the
142+
# strategy is broken, skip the test.
143+
if "strategy" in item.fixturenames:
144+
env = item.config.stash[LABGRID_ENV_KEY]
145+
# skip test even if only one of the targets in the env has a broken strategy
146+
for target_name in env.config.get_targets():
147+
target = env.get_target(target_name)
148+
try:
149+
strategy = target.get_strategy()
150+
if strategy.broken:
151+
pytest.skip(f"{strategy.__class__.__name__} is in broken state")
152+
except NoStrategyFoundError:
153+
pass

0 commit comments

Comments
 (0)