Skip to content

Commit bd0cff0

Browse files
Bastian-KrauseEmantor
authored andcommitted
pytestplugin/hooks: improve @pytest.mark.lg_feature() descriptions and error messages
Explain the purpose of the marker, turn the bare Exception into a pytest.UsageError, improve and simplify the error message and skip reason. Signed-off-by: Bastian Krause <bst@pengutronix.de>
1 parent fd3f528 commit bd0cff0

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

labgrid/pytestplugin/hooks.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def pytest_configure(config):
7171
configure_pytest_logging(config, logging_plugin)
7272

7373
config.addinivalue_line("markers",
74-
"lg_feature: marker for labgrid feature flags")
74+
"lg_feature: skip tests on envs/targets without given labgrid feature flags")
7575
lg_log = config.option.lg_log
7676
if lg_log:
7777
ConsoleLoggingReporter(lg_log)
@@ -101,27 +101,24 @@ def pytest_collection_modifyitems(config, items):
101101
have_feature = env.get_features() | env.get_target_features()
102102

103103
for item in items:
104+
# pytest.mark.lg_feature
105+
lg_feature_signature = "pytest.mark.lg_feature(features: str | list[str])"
104106
want_feature = set()
105107

106108
for marker in item.iter_markers("lg_feature"):
107-
arg = marker.args[0]
108-
if isinstance(arg, str):
109-
want_feature.add(arg)
110-
elif isinstance(arg, list):
111-
want_feature.update(arg)
109+
if len(marker.args) != 1 or marker.kwargs:
110+
raise pytest.UsageError(f"Unexpected number of args/kwargs for {lg_feature_signature}")
111+
elif isinstance(marker.args[0], str):
112+
want_feature.add(marker.args[0])
113+
elif isinstance(marker.args[0], list):
114+
want_feature.update(marker.args[0])
112115
else:
113-
raise Exception("Unsupported feature argument type")
116+
raise pytest.UsageError(f"Unsupported 'features' argument type ({type(marker.args[0])}) for {lg_feature_signature}")
117+
114118
missing_feature = want_feature - have_feature
115119
if missing_feature:
116-
if len(missing_feature) == 1:
117-
skip = pytest.mark.skip(
118-
reason=f'Skipping because feature "{missing_feature}" is not supported'
119-
)
120-
else:
121-
skip = pytest.mark.skip(
122-
reason=f'Skipping because features "{missing_feature}" are not supported'
123-
)
124-
item.add_marker(skip)
120+
reason = f'unsupported feature(s): {", ".join(missing_feature)}'
121+
item.add_marker(pytest.mark.skip(reason=reason))
125122

126123
@pytest.hookimpl(tryfirst=True)
127124
def pytest_runtest_setup(item):

0 commit comments

Comments
 (0)