Skip to content

Commit 465b2d9

Browse files
authored
Further "unknown marks warning" improvements (#5178)
Further "unknown marks warning" improvements
2 parents 184ef92 + 0594dba commit 465b2d9

20 files changed

+106
-78
lines changed

changelog/5023.feature.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
New flag ``--strict-markers`` that triggers an error when unknown markers (e.g. those not registered using the `markers option`_ in the configuration file) are used in the test suite.
2+
3+
The existing ``--strict`` option has the same behavior currently, but can be augmented in the future for additional checks.
4+
5+
.. _`markers option`: https://docs.pytest.org/en/latest/reference.html#confval-markers

doc/en/example/markers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ For an example on how to add and work with markers from a plugin, see
259259
* Asking for existing markers via ``pytest --markers`` gives good output
260260

261261
* Typos in function markers are treated as an error if you use
262-
the ``--strict`` option.
262+
the ``--strict-markers`` option.
263263

264264
.. _`scoped-marking`:
265265

doc/en/mark.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ marks by registering them in ``pytest.ini`` like this:
4141
slow
4242
serial
4343
44-
When the ``--strict`` command-line flag is passed, any unknown marks applied
44+
When the ``--strict-markers`` command-line flag is passed, any unknown marks applied
4545
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error.
4646
Marks added by pytest or by a plugin instead of the decorator will not trigger
47-
this error. To enforce validation of markers, add ``--strict`` to ``addopts``:
47+
this error. To enforce validation of markers, add ``--strict-markers`` to ``addopts``:
4848

4949
.. code-block:: ini
5050
5151
[pytest]
52-
addopts = --strict
52+
addopts = --strict-markers
5353
markers =
5454
slow
5555
serial

doc/en/reference.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,15 +1269,17 @@ passed multiple times. The expected format is ``name=value``. For example::
12691269

12701270
.. confval:: markers
12711271

1272-
When the ``--strict`` command-line argument is used, only known markers -
1273-
defined in code by core pytest or some plugin - are allowed.
1274-
You can list additional markers in this setting to add them to the whitelist.
1272+
When the ``--strict-markers`` or ``--strict`` command-line arguments are used,
1273+
only known markers - defined in code by core pytest or some plugin - are allowed.
12751274

1276-
You can list one marker name per line, indented from the option name.
1275+
You can list additional markers in this setting to add them to the whitelist,
1276+
in which case you probably want to add ``--strict-markers`` to ``addopts``
1277+
to avoid future regressions:
12771278

12781279
.. code-block:: ini
12791280
12801281
[pytest]
1282+
addopts = --strict-markers
12811283
markers =
12821284
slow
12831285
serial

src/_pytest/main.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ def pytest_addoption(parser):
4747
type="args",
4848
default=[],
4949
)
50-
# parser.addini("dirpatterns",
51-
# "patterns specifying possible locations of test files",
52-
# type="linelist", default=["**/test_*.txt",
53-
# "**/test_*.py", "**/*_test.py"]
54-
# )
5550
group = parser.getgroup("general", "running and selection options")
5651
group._addoption(
5752
"-x",
@@ -71,9 +66,10 @@ def pytest_addoption(parser):
7166
help="exit after first num failures or errors.",
7267
)
7368
group._addoption(
69+
"--strict-markers",
7470
"--strict",
7571
action="store_true",
76-
help="marks not registered in configuration file raise errors.",
72+
help="markers not registered in the `markers` section of the configuration file raise errors.",
7773
)
7874
group._addoption(
7975
"-c",

src/_pytest/mark/structures.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ def __getattr__(self, name):
311311
# If the name is not in the set of known marks after updating,
312312
# then it really is time to issue a warning or an error.
313313
if name not in self._markers:
314-
if self._config.option.strict:
315-
fail("{!r} is not a registered marker".format(name), pytrace=False)
314+
if self._config.option.strict_markers:
315+
fail(
316+
"{!r} not found in `markers` configuration option".format(name),
317+
pytrace=False,
318+
)
316319
else:
317320
warnings.warn(
318321
"Unknown pytest.mark.%s - is this a typo? You can register "

testing/code/test_excinfo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import py
1212
import six
1313
from six.moves import queue
14-
from test_source import astonly
1514

1615
import _pytest
1716
import pytest
@@ -147,7 +146,6 @@ def test_traceback_entry_getsource(self):
147146
assert s.startswith("def f():")
148147
assert s.endswith("raise ValueError")
149148

150-
@astonly
151149
@failsonjython
152150
def test_traceback_entry_getsource_in_construct(self):
153151
source = _pytest._code.Source(

testing/code/test_source.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import pytest
1717
from _pytest._code import Source
1818

19-
astonly = pytest.mark.nothing
2019
failsonjython = pytest.mark.xfail("sys.platform.startswith('java')")
2120

2221

@@ -227,7 +226,6 @@ def test_getstatementrange_triple_quoted(self):
227226
s = source.getstatement(1)
228227
assert s == str(source)
229228

230-
@astonly
231229
def test_getstatementrange_within_constructs(self):
232230
source = Source(
233231
"""\
@@ -630,7 +628,6 @@ def test_multiline():
630628

631629

632630
class TestTry(object):
633-
pytestmark = astonly
634631
source = """\
635632
try:
636633
raise ValueError
@@ -675,7 +672,6 @@ def test_finally(self):
675672

676673

677674
class TestIf(object):
678-
pytestmark = astonly
679675
source = """\
680676
if 1:
681677
y = 3

testing/python/fixtures.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,10 +1925,10 @@ def test_hello(arg1):
19251925
reprec = testdir.inline_run()
19261926
reprec.assertoutcome(passed=1)
19271927

1928-
@pytest.mark.issue(226)
19291928
@pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"])
19301929
@pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"])
19311930
def test_ordering_dependencies_torndown_first(self, testdir, param1, param2):
1931+
"""#226"""
19321932
testdir.makepyfile(
19331933
"""
19341934
import pytest
@@ -2707,9 +2707,9 @@ def test_3():
27072707
reprec = testdir.inline_run("-v")
27082708
reprec.assertoutcome(passed=5)
27092709

2710-
@pytest.mark.issue(246)
27112710
@pytest.mark.parametrize("scope", ["session", "function", "module"])
27122711
def test_finalizer_order_on_parametrization(self, scope, testdir):
2712+
"""#246"""
27132713
testdir.makepyfile(
27142714
"""
27152715
import pytest
@@ -2744,8 +2744,8 @@ def test_other():
27442744
reprec = testdir.inline_run("-lvs")
27452745
reprec.assertoutcome(passed=3)
27462746

2747-
@pytest.mark.issue(396)
27482747
def test_class_scope_parametrization_ordering(self, testdir):
2748+
"""#396"""
27492749
testdir.makepyfile(
27502750
"""
27512751
import pytest
@@ -2865,8 +2865,8 @@ def test_foo(fix):
28652865
res = testdir.runpytest("-v")
28662866
res.stdout.fnmatch_lines(["*test_foo*alpha*", "*test_foo*beta*"])
28672867

2868-
@pytest.mark.issue(920)
28692868
def test_deterministic_fixture_collection(self, testdir, monkeypatch):
2869+
"""#920"""
28702870
testdir.makepyfile(
28712871
"""
28722872
import pytest
@@ -3649,7 +3649,6 @@ class TestScopeOrdering(object):
36493649
"""Class of tests that ensure fixtures are ordered based on their scopes (#2405)"""
36503650

36513651
@pytest.mark.parametrize("variant", ["mark", "autouse"])
3652-
@pytest.mark.issue(github="#2405")
36533652
def test_func_closure_module_auto(self, testdir, variant, monkeypatch):
36543653
"""Semantically identical to the example posted in #2405 when ``use_mark=True``"""
36553654
monkeypatch.setenv("FIXTURE_ACTIVATION_VARIANT", variant)

testing/python/integration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,9 @@ def test_blah(self):
393393
assert not call.items
394394

395395

396-
@pytest.mark.issue(351)
397396
class TestParameterize(object):
397+
"""#351"""
398+
398399
def test_idfn_marker(self, testdir):
399400
testdir.makepyfile(
400401
"""

0 commit comments

Comments
 (0)