Change ``--strict`` to ``--strict-markers``, preserving the old one

Fix #5023
This commit is contained in:
Bruno Oliveira 2019-04-27 10:52:12 -03:00
parent ceca35b94a
commit 685ca96c71
9 changed files with 50 additions and 29 deletions

View File

@ -0,0 +1 @@
``--strict`` is now called ``--strict-markers`` as it is more explicit about what it does. The old name still works for backward compatibility.

View File

@ -259,7 +259,7 @@ For an example on how to add and work with markers from a plugin, see
* Asking for existing markers via ``pytest --markers`` gives good output
* Typos in function markers are treated as an error if you use
the ``--strict`` option.
the ``--strict-markers`` option.
.. _`scoped-marking`:

View File

@ -41,15 +41,15 @@ marks by registering them in ``pytest.ini`` like this:
slow
serial
When the ``--strict`` command-line flag is passed, any unknown marks applied
When the ``--strict-markers`` command-line flag is passed, any unknown marks applied
with the ``@pytest.mark.name_of_the_mark`` decorator will trigger an error.
Marks added by pytest or by a plugin instead of the decorator will not trigger
this error. To enforce validation of markers, add ``--strict`` to ``addopts``:
this error. To enforce validation of markers, add ``--strict-markers`` to ``addopts``:
.. code-block:: ini
[pytest]
addopts = --strict
addopts = --strict-markers
markers =
slow
serial

View File

@ -1261,19 +1261,24 @@ passed multiple times. The expected format is ``name=value``. For example::
.. confval:: markers
When the ``--strict`` command-line argument is used, only known markers -
When the ``--strict-markers`` command-line argument is used, only known markers -
defined in code by core pytest or some plugin - are allowed.
You can list additional markers in this setting to add them to the whitelist.
You can list one marker name per line, indented from the option name.
You can list additional markers in this setting to add them to the whitelist,
in which case you probably want to add ``--strict-markers`` to ``addopts``
to avoid future regressions:
.. code-block:: ini
[pytest]
addopts = --strict-markers
markers =
slow
serial
**Note**: This option was previously called ``--strict``, which is now an
alias preserved for backward compatibility.
.. confval:: minversion
Specifies a minimal pytest version required for running tests.

View File

@ -47,11 +47,6 @@ def pytest_addoption(parser):
type="args",
default=[],
)
# parser.addini("dirpatterns",
# "patterns specifying possible locations of test files",
# type="linelist", default=["**/test_*.txt",
# "**/test_*.py", "**/*_test.py"]
# )
group = parser.getgroup("general", "running and selection options")
group._addoption(
"-x",
@ -71,9 +66,10 @@ def pytest_addoption(parser):
help="exit after first num failures or errors.",
)
group._addoption(
"--strict-markers",
"--strict",
action="store_true",
help="marks not registered in configuration file raise errors.",
help="markers not registered in the `markers` section of the configuration file raise errors.",
)
group._addoption(
"-c",

View File

@ -311,8 +311,11 @@ class MarkGenerator(object):
# If the name is not in the set of known marks after updating,
# then it really is time to issue a warning or an error.
if name not in self._markers:
if self._config.option.strict:
fail("{!r} is not a registered marker".format(name), pytrace=False)
if self._config.option.strict_markers:
fail(
"{!r} not found in `markers` configuration option".format(name),
pytrace=False,
)
else:
warnings.warn(
"Unknown pytest.mark.%s - is this a typo? You can register "

View File

@ -130,7 +130,7 @@ def test_ini_markers_whitespace(testdir):
assert True
"""
)
rec = testdir.inline_run("--strict", "-m", "a1")
rec = testdir.inline_run("--strict-markers", "-m", "a1")
rec.assertoutcome(passed=1)
@ -150,7 +150,7 @@ def test_marker_without_description(testdir):
)
ftdir = testdir.mkdir("ft1_dummy")
testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py"))
rec = testdir.runpytest("--strict")
rec = testdir.runpytest("--strict-markers")
rec.assert_outcomes()
@ -194,7 +194,8 @@ def test_mark_on_pseudo_function(testdir):
reprec.assertoutcome(passed=1)
def test_strict_prohibits_unregistered_markers(testdir):
@pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"])
def test_strict_prohibits_unregistered_markers(testdir, option_name):
testdir.makepyfile(
"""
import pytest
@ -203,9 +204,11 @@ def test_strict_prohibits_unregistered_markers(testdir):
pass
"""
)
result = testdir.runpytest("--strict")
result = testdir.runpytest(option_name)
assert result.ret != 0
result.stdout.fnmatch_lines(["'unregisteredmark' is not a registered marker"])
result.stdout.fnmatch_lines(
["'unregisteredmark' not found in `markers` configuration option"]
)
@pytest.mark.parametrize(

View File

@ -76,7 +76,7 @@ def broken_testdir(testdir):
def test_run_without_stepwise(stepwise_testdir):
result = stepwise_testdir.runpytest("-v", "--strict", "--fail")
result = stepwise_testdir.runpytest("-v", "--strict-markers", "--fail")
result.stdout.fnmatch_lines(["*test_success_before_fail PASSED*"])
result.stdout.fnmatch_lines(["*test_fail_on_flag FAILED*"])
@ -85,7 +85,9 @@ def test_run_without_stepwise(stepwise_testdir):
def test_fail_and_continue_with_stepwise(stepwise_testdir):
# Run the tests with a failing second test.
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "--fail")
result = stepwise_testdir.runpytest(
"-v", "--strict-markers", "--stepwise", "--fail"
)
assert not result.stderr.str()
stdout = result.stdout.str()
@ -95,7 +97,7 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
assert "test_success_after_fail" not in stdout
# "Fix" the test that failed in the last run and run it again.
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise")
result = stepwise_testdir.runpytest("-v", "--strict-markers", "--stepwise")
assert not result.stderr.str()
stdout = result.stdout.str()
@ -107,7 +109,12 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
def test_run_with_skip_option(stepwise_testdir):
result = stepwise_testdir.runpytest(
"-v", "--strict", "--stepwise", "--stepwise-skip", "--fail", "--fail-last"
"-v",
"--strict-markers",
"--stepwise",
"--stepwise-skip",
"--fail",
"--fail-last",
)
assert not result.stderr.str()
@ -120,7 +127,7 @@ def test_run_with_skip_option(stepwise_testdir):
def test_fail_on_errors(error_testdir):
result = error_testdir.runpytest("-v", "--strict", "--stepwise")
result = error_testdir.runpytest("-v", "--strict-markers", "--stepwise")
assert not result.stderr.str()
stdout = result.stdout.str()
@ -131,7 +138,7 @@ def test_fail_on_errors(error_testdir):
def test_change_testfile(stepwise_testdir):
result = stepwise_testdir.runpytest(
"-v", "--strict", "--stepwise", "--fail", "test_a.py"
"-v", "--strict-markers", "--stepwise", "--fail", "test_a.py"
)
assert not result.stderr.str()
@ -140,7 +147,9 @@ def test_change_testfile(stepwise_testdir):
# Make sure the second test run starts from the beginning, since the
# test to continue from does not exist in testfile_b.
result = stepwise_testdir.runpytest("-v", "--strict", "--stepwise", "test_b.py")
result = stepwise_testdir.runpytest(
"-v", "--strict-markers", "--stepwise", "test_b.py"
)
assert not result.stderr.str()
stdout = result.stdout.str()
@ -149,7 +158,11 @@ def test_change_testfile(stepwise_testdir):
def test_stop_on_collection_errors(broken_testdir):
result = broken_testdir.runpytest(
"-v", "--strict", "--stepwise", "working_testfile.py", "broken_testfile.py"
"-v",
"--strict-markers",
"--stepwise",
"working_testfile.py",
"broken_testfile.py",
)
stdout = result.stdout.str()

View File

@ -302,7 +302,7 @@ def test_filterwarnings_mark_registration(testdir):
pass
"""
)
result = testdir.runpytest("--strict")
result = testdir.runpytest("--strict-markers")
assert result.ret == 0