diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index 4ff6ce70713..31b73a2c927 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -1054,7 +1054,6 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None: args, namespace=copy.copy(self.option) ) self._validate_plugins() - self._validate_keys() if self.known_args_namespace.confcutdir is None and self.inifile: confcutdir = py.path.local(self.inifile).dirname self.known_args_namespace.confcutdir = confcutdir @@ -1077,6 +1076,7 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None: ) else: raise + self._validate_keys() def _checkversion(self): import pytest diff --git a/testing/test_config.py b/testing/test_config.py index d59d641f6ab..c9eea7a1675 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -203,6 +203,15 @@ def test_confcutdir(self, testdir): """ [pytest] minversion = 5.0.0 + """, + [], + [], + "", + ), + ( + """ + [pytest] + conftest_ini_key = 1 """, [], [], @@ -213,16 +222,25 @@ def test_confcutdir(self, testdir): def test_invalid_ini_keys( self, testdir, ini_file_text, invalid_keys, stderr_output, exception_text ): + testdir.makeconftest( + """ + def pytest_addoption(parser): + parser.addini("conftest_ini_key", "") + """ + ) testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text)) + config = testdir.parseconfig() assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys) result = testdir.runpytest() result.stderr.fnmatch_lines(stderr_output) - if stderr_output: + if exception_text: with pytest.raises(pytest.fail.Exception, match=exception_text): testdir.runpytest("--strict-config") + else: + testdir.runpytest("--strict-config") @pytest.mark.parametrize( "ini_file_text, exception_text",