Skip to content

Commit b21df50

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 bc2ce83 commit b21df50

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,22 @@ def pytest_collection_modifyitems(config, items):
130130
reason=f'Skipping because features "{missing_feature}" are not supported'
131131
)
132132
item.add_marker(skip)
133+
134+
@pytest.hookimpl(wrapper=True)
135+
def pytest_runtest_setup(item):
136+
"""
137+
Skip test if one of the targets uses a strategy considered broken.
138+
"""
139+
# Before any fixtures run for the test, check if the session-scoped strategy fixture was
140+
# requested (might have been executed already for a prior test). If that's the case and the
141+
# strategy is broken, skip the test.
142+
if "strategy" in item.fixturenames:
143+
env = item.config.stash[LABGRID_ENV_KEY]
144+
# skip test even if only one of the targets in the env has a broken strategy
145+
for target_name in env.config.get_targets():
146+
target = env.get_target(target_name)
147+
strategy = target.get_driver("Strategy")
148+
if strategy and strategy.broken:
149+
pytest.skip(f"{strategy.__class__.__name__} is in broken state")
150+
151+
yield

0 commit comments

Comments
 (0)