Skip to content

Commit f63c683

Browse files
committed
No longer escape regex in pytest.mark.filterwarnings
Fix pytest-dev#3936
1 parent ddb3084 commit f63c683

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

changelog/3936.removal.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
``@pytest.mark.filterwarnings`` second parameter is no longer regex-escaped,
2+
making it possible to actually use regular expressions to check the warning message.
3+
4+
**Note**: regex-escaping the match string was an implementation oversight that might break test suites which depend
5+
on the old behavior.

src/_pytest/warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def catch_warnings_for_item(config, ihook, when, item):
8181
if item is not None:
8282
for mark in item.iter_markers(name="filterwarnings"):
8383
for arg in mark.args:
84-
warnings._setoption(arg)
84+
_setoption(warnings, arg)
8585
filters_configured = True
8686

8787
if not filters_configured:

testing/test_warnings.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ def test_foo():
374374
)
375375

376376

377+
@pytest.mark.filterwarnings("always")
378+
def test_mark_regex_escape(testdir):
379+
"""@pytest.mark.filterwarnings should not try to escape regex characters (#3936)"""
380+
testdir.makepyfile(
381+
r"""
382+
import pytest, warnings
383+
384+
@pytest.mark.filterwarnings(r"ignore:some \(warning\)")
385+
def test_foo():
386+
warnings.warn(UserWarning("some (warning)"))
387+
"""
388+
)
389+
result = testdir.runpytest()
390+
assert WARNINGS_SUMMARY_HEADER not in result.stdout.str()
391+
392+
377393
@pytest.mark.filterwarnings("default")
378394
@pytest.mark.parametrize("ignore_pytest_warnings", ["no", "ini", "cmdline"])
379395
def test_hide_pytest_internal_warnings(testdir, ignore_pytest_warnings):

0 commit comments

Comments
 (0)