Skip to content

Commit ab40696

Browse files
nicoddemusblueyed
authored andcommitted
-W now takes precedence over filters in ini files
Fix pytest-dev#3946
1 parent d12f46c commit ab40696

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

changelog/3946.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Warning filters passed as command line options using ``-W`` now take precedence over filters defined in ``ini``
2+
configuration files.

src/_pytest/warnings.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,19 @@ def catch_warnings_for_item(config, ihook, when, item):
6767
6868
Each warning captured triggers the ``pytest_warning_captured`` hook.
6969
"""
70-
args = config.getoption("pythonwarnings") or []
70+
cmdline_filters = config.getoption("pythonwarnings") or []
7171
inifilters = config.getini("filterwarnings")
7272
with warnings.catch_warnings(record=True) as log:
73-
filters_configured = args or inifilters or sys.warnoptions
74-
75-
for arg in args:
76-
warnings._setoption(arg)
73+
filters_configured = bool(cmdline_filters or inifilters or sys.warnoptions)
7774

75+
# filters should have this precedence: mark, cmdline options, ini
76+
# filters should be applied in the inverse order of precedence
7877
for arg in inifilters:
7978
_setoption(warnings, arg)
8079

80+
for arg in cmdline_filters:
81+
warnings._setoption(arg)
82+
8183
if item is not None:
8284
for mark in item.iter_markers(name="filterwarnings"):
8385
for arg in mark.args:

testing/test_warnings.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,50 @@ def test_bar():
430430
)
431431

432432

433+
@pytest.mark.parametrize("ignore_on_cmdline", [True, False])
434+
def test_option_precedence_cmdline_over_ini(testdir, ignore_on_cmdline):
435+
"""filters defined in the command-line should take precedence over filters in ini files (#3946)."""
436+
testdir.makeini(
437+
"""
438+
[pytest]
439+
filterwarnings = error
440+
"""
441+
)
442+
testdir.makepyfile(
443+
"""
444+
import warnings
445+
def test():
446+
warnings.warn(UserWarning('hello'))
447+
"""
448+
)
449+
args = ["-W", "ignore"] if ignore_on_cmdline else []
450+
result = testdir.runpytest(*args)
451+
if ignore_on_cmdline:
452+
result.stdout.fnmatch_lines(["* 1 passed in*"])
453+
else:
454+
result.stdout.fnmatch_lines(["* 1 failed in*"])
455+
456+
457+
def test_option_precedence_mark(testdir):
458+
"""Filters defined by marks should always take precedence (#3946)."""
459+
testdir.makeini(
460+
"""
461+
[pytest]
462+
filterwarnings = ignore
463+
"""
464+
)
465+
testdir.makepyfile(
466+
"""
467+
import pytest, warnings
468+
@pytest.mark.filterwarnings('error')
469+
def test():
470+
warnings.warn(UserWarning('hello'))
471+
"""
472+
)
473+
result = testdir.runpytest("-W", "ignore")
474+
result.stdout.fnmatch_lines(["* 1 failed in*"])
475+
476+
433477
class TestDeprecationWarningsByDefault:
434478
"""
435479
Note: all pytest runs are executed in a subprocess so we don't inherit warning filters

0 commit comments

Comments
 (0)