diff --git a/AUTHORS b/AUTHORS index ba5672d4c51..093d5fd472a 100644 --- a/AUTHORS +++ b/AUTHORS @@ -87,6 +87,7 @@ croc100 Cal Leeming Carl Friedrich Bolz Carlos Jenkins +Cen Fangyu (Dmao233) Ceridwen Charles Cloud Charles Machalow diff --git a/changelog/14953.bugfix.rst b/changelog/14953.bugfix.rst new file mode 100644 index 00000000000..bacb105878e --- /dev/null +++ b/changelog/14953.bugfix.rst @@ -0,0 +1 @@ +The :confval:`max_warnings` configuration option now accepts integer values in TOML configuration files, while still accepting strings for backward compatibility. diff --git a/doc/en/reference/reference.rst b/doc/en/reference/reference.rst index 362d5c917e2..cb99744330d 100644 --- a/doc/en/reference/reference.rst +++ b/doc/en/reference/reference.rst @@ -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 @@ -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 diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 1b337e20c7e..adfdb37dbe6 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -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") diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index 825435225b3..481cbc96900 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -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. diff --git a/testing/test_warnings.py b/testing/test_warnings.py index 017781c2355..0344cb453d8 100644 --- a/testing/test_warnings.py +++ b/testing/test_warnings.py @@ -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."""