Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ croc100
Cal Leeming
Carl Friedrich Bolz
Carlos Jenkins
Cen Fangyu (Dmao233)
Ceridwen
Charles Cloud
Charles Machalow
Expand Down
1 change: 1 addition & 0 deletions changelog/14953.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :confval:`max_warnings` configuration option now accepts integer values in TOML configuration files, while still accepting strings for backward compatibility.
6 changes: 4 additions & 2 deletions doc/en/reference/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1705,9 +1705,11 @@ passed multiple times. The expected format is ``name=value``. For example::


.. confval:: max_warnings
:type: ``int``
:type: ``int | str``

.. versionadded:: 9.1
.. versionchanged:: 9.2
Added support for specifying the value as an integer in TOML configuration.

Maximum number of warnings allowed before the test run is considered a failure.
When all tests pass, but the total number of warnings exceeds this value, pytest exits with
Expand Down Expand Up @@ -3671,7 +3673,7 @@ All the command-line flags can also be obtained by running ``pytest --help``::
Each line specifies a pattern for
warnings.filterwarnings. Processed after
-W/--pythonwarnings.
max_warnings (string):
max_warnings (int | string):
Exit with error if all tests pass but the number of
warnings exceeds this threshold
norecursedirs (args): Directory patterns to avoid for recursion
Expand Down
2 changes: 2 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ def pytest_addoption(parser: Parser) -> None:
parser.addini(
"max_warnings",
help="Exit with error if all tests pass but the number of warnings exceeds this threshold",
type=int | str,
default=None,
)

group = parser.getgroup("collect", "collection")
Expand Down
6 changes: 3 additions & 3 deletions src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,10 +1092,10 @@ def _get_max_warnings(self) -> int | None:
value = self.config.option.max_warnings
if value is not None:
return int(value)
ini_value = self.config.getini("max_warnings")
if ini_value:
ini_value: int | str | None = self.config.getini("max_warnings")
if isinstance(ini_value, str):
return int(ini_value)
return None
return ini_value

#
# Summaries for sessionfinish.
Expand Down
17 changes: 17 additions & 0 deletions testing/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,23 @@ def test_max_warnings_ini_option(self, pytester: Pytester) -> None:
result = pytester.runpytest()
assert result.ret == ExitCode.MAX_WARNINGS_ERROR

@pytest.mark.filterwarnings("default::UserWarning")
@pytest.mark.parametrize("value", ["1", '"1"'])
def test_max_warnings_toml_option(self, pytester: Pytester, value: str) -> None:
"""max_warnings can be set via TOML configuration.

Supports both int and str (for backward compat).
"""
pytester.maketoml(
f"""
[pytest]
max_warnings = {value}
"""
)
pytester.makepyfile(self.PYFILE)
result = pytester.runpytest()
assert result.ret == ExitCode.MAX_WARNINGS_ERROR

@pytest.mark.filterwarnings("default::UserWarning")
def test_max_warnings_with_test_failure(self, pytester: Pytester) -> None:
"""When tests fail AND warnings exceed max, TESTS_FAILED takes priority."""
Expand Down