Skip to content

Commit

Permalink
Change --strict to --strict-markers, preserving the old one
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Apr 27, 2019
1 parent ebc0cea commit 645b82b
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog/5023.feature.rst
@@ -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.
2 changes: 1 addition & 1 deletion doc/en/example/markers.rst
Expand Up @@ -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`:

Expand Down
6 changes: 3 additions & 3 deletions doc/en/mark.rst
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions doc/en/reference.rst
Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions src/_pytest/main.py
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/_pytest/mark/structures.py
Expand Up @@ -311,8 +311,11 @@ def __getattr__(self, name):
# 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 "
Expand Down
13 changes: 8 additions & 5 deletions testing/test_mark.py
Expand Up @@ -130,7 +130,7 @@ def test_markers():
assert True
"""
)
rec = testdir.inline_run("--strict", "-m", "a1")
rec = testdir.inline_run("--strict-markers", "-m", "a1")
rec.assertoutcome(passed=1)


Expand All @@ -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()


Expand Down Expand Up @@ -194,7 +194,8 @@ def test_hello():
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
Expand All @@ -203,9 +204,11 @@ def test_hello():
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(
Expand Down
29 changes: 21 additions & 8 deletions testing/test_stepwise.py
Expand Up @@ -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*"])
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion testing/test_warnings.py
Expand Up @@ -302,7 +302,7 @@ def test_func():
pass
"""
)
result = testdir.runpytest("--strict")
result = testdir.runpytest("--strict-markers")
assert result.ret == 0


Expand Down

0 comments on commit 645b82b

Please sign in to comment.