diff --git a/changelog/5023.feature.rst b/changelog/5023.feature.rst new file mode 100644 index 00000000000..a4c67fe6856 --- /dev/null +++ b/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. diff --git a/doc/en/example/markers.rst b/doc/en/example/markers.rst index 6275bbae068..aa9eb22e2a3 100644 --- a/doc/en/example/markers.rst +++ b/doc/en/example/markers.rst @@ -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`: diff --git a/doc/en/mark.rst b/doc/en/mark.rst index ad9aaca4b9a..b9f73a93aea 100644 --- a/doc/en/mark.rst +++ b/doc/en/mark.rst @@ -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 diff --git a/doc/en/reference.rst b/doc/en/reference.rst index 12e0d09a8be..6f790a650b5 100644 --- a/doc/en/reference.rst +++ b/doc/en/reference.rst @@ -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. diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 64e7110b4a2..a2041007de7 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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", diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index 4cae97b7135..2f683fa08dd 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -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 " diff --git a/testing/test_mark.py b/testing/test_mark.py index 72b96ab518c..294a1ee6bc8 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -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) @@ -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_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 @@ -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( diff --git a/testing/test_stepwise.py b/testing/test_stepwise.py index b8583992577..2202bbf1b60 100644 --- a/testing/test_stepwise.py +++ b/testing/test_stepwise.py @@ -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() diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 929ae8d607f..ce5fca98a1f 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -302,7 +302,7 @@ def test_func(): pass """ ) - result = testdir.runpytest("--strict") + result = testdir.runpytest("--strict-markers") assert result.ret == 0