Skip to content

Commit

Permalink
Unknown ini key warnings is delayed so it can be filtered out
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoddemus committed Aug 15, 2020
1 parent 3f0abcc commit 13ad39c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog/7620.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It is now possible to silence :class:`pytest.PytestConfigWarning` messages about unknown configuration keys by using :confval:`filterwarnings`.
16 changes: 9 additions & 7 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,12 @@ def _preparse(self, args: List[str], addopts: bool = True) -> None:
)
else:
raise
self._validate_keys()

@hookimpl(hookwrapper=True)
def pytest_collection(self):
"""Validate invalid ini keys after collection is done."""
yield
self._validate_config_keys()

def _checkversion(self) -> None:
import pytest
Expand All @@ -1148,7 +1153,7 @@ def _checkversion(self) -> None:
% (self.inifile, minver, pytest.__version__,)
)

def _validate_keys(self) -> None:
def _validate_config_keys(self) -> None:
for key in sorted(self._get_unknown_ini_keys()):
self._warn_or_fail_if_strict("Unknown config ini key: {}\n".format(key))

Expand Down Expand Up @@ -1185,14 +1190,11 @@ def _validate_plugins(self) -> None:
)

def _warn_or_fail_if_strict(self, message: str) -> None:
"""Issue a PytestConfigWarning or fail if --strict-config was given"""
if self.known_args_namespace.strict_config:
fail(message, pytrace=False)

from _pytest.warnings import _issue_warning_captured

_issue_warning_captured(
PytestConfigWarning(message), self.hook, stacklevel=3,
)
warnings.warn(PytestConfigWarning(message))

def _get_unknown_ini_keys(self) -> List[str]:
parser_inicfg = self._parser._inidict
Expand Down
20 changes: 16 additions & 4 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def test_confcutdir(self, testdir):
),
],
)
@pytest.mark.filterwarnings("default")
def test_invalid_ini_keys(
self, testdir, ini_file_text, invalid_keys, warning_output, exception_text
):
Expand All @@ -250,10 +251,21 @@ def pytest_addoption(parser):
result.stdout.fnmatch_lines(warning_output)

if exception_text:
with pytest.raises(pytest.fail.Exception, match=exception_text):
testdir.runpytest("--strict-config")
else:
testdir.runpytest("--strict-config")
result = testdir.runpytest("--strict-config")
result.stdout.fnmatch_lines("INTERNALERROR>*" + exception_text)

@pytest.mark.filterwarnings("default")
def test_silence_unknown_key_warning(self, testdir):
testdir.makeini(
"""
[pytest]
filterwarnings =
ignore:Unknown config ini key:pytest.PytestConfigWarning
foobar=1
"""
)
result = testdir.runpytest()
result.stdout.no_fnmatch_line("*PytestConfigWarning*")

@pytest.mark.parametrize(
"ini_file_text, exception_text",
Expand Down

0 comments on commit 13ad39c

Please sign in to comment.