Skip to content

Commit 9121138

Browse files
committed
Emit warning for unknown marks
1 parent 407d74b commit 9121138

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

changelog/4826.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A warning is now emitted when unknown marks are used as a decorator.
2+
This is often due to a typo, which can lead to silently broken tests.

doc/en/mark.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ which also serve as documentation.
3131
Raising errors on unknown marks
3232
-------------------------------
3333

34-
Marks can be registered in ``pytest.ini`` like this:
34+
Unknown marks applied with the ``@pytest.mark.name_of_the_mark`` decorator
35+
will always emit a warning, in order to avoid silently doing something
36+
surprising due to mis-typed names. You can disable the warning for custom
37+
marks by registering them in ``pytest.ini`` like this:
3538

3639
.. code-block:: ini
3740

src/_pytest/mark/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def pytest_collection_modifyitems(items, config):
147147

148148
def pytest_configure(config):
149149
config._old_mark_config = MARK_GEN._config
150-
if config.option.strict:
151-
MARK_GEN._config = config
150+
MARK_GEN._config = config
152151

153152
empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION)
154153

src/_pytest/mark/structures.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ..compat import MappingMixin
1212
from ..compat import NOTSET
1313
from _pytest.outcomes import fail
14+
from _pytest.warning_types import PytestWarning
1415

1516
EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
1617

@@ -283,28 +284,37 @@ def test_function():
283284
on the ``test_function`` object. """
284285

285286
_config = None
287+
_markers = set()
286288

287289
def __getattr__(self, name):
288290
if name[0] == "_":
289291
raise AttributeError("Marker name must NOT start with underscore")
292+
290293
if self._config is not None:
291-
self._check(name)
294+
self._update_markers(name)
295+
if name not in self._markers:
296+
warnings.warn(
297+
"Unknown mark %r. You can register custom marks to avoid this "
298+
"warning, without risking typos that break your tests. See "
299+
"https://docs.pytest.org/en/latest/mark.html for details." % name,
300+
PytestWarning,
301+
)
302+
if self._config.option.strict:
303+
fail("{!r} not a registered marker".format(name), pytrace=False)
304+
292305
return MarkDecorator(Mark(name, (), {}))
293306

294-
def _check(self, name):
295-
try:
296-
if name in self._markers:
297-
return
298-
except AttributeError:
299-
pass
300-
self._markers = values = set()
301-
for line in self._config.getini("markers"):
302-
marker = line.split(":", 1)[0]
303-
marker = marker.rstrip()
304-
x = marker.split("(", 1)[0]
305-
values.add(x)
307+
def _update_markers(self, name):
308+
# We store a set of registered markers as a performance optimisation,
309+
# but more could be added to `self._config` by other plugins at runtime.
310+
# If we see an unknown marker, we therefore update the set and try again!
306311
if name not in self._markers:
307-
fail("{!r} not a registered marker".format(name), pytrace=False)
312+
for line in self._config.getini("markers"):
313+
# example lines: "skipif(condition): skip the given test if..."
314+
# or "hypothesis: tests which use Hypothesis", so to get the
315+
# marker name we we split on both `:` and `(`.
316+
marker = line.split(":")[0].split("(")[0].strip()
317+
self._markers.add(marker)
308318

309319

310320
MARK_GEN = MarkGenerator()

0 commit comments

Comments
 (0)