Skip to content

Make assertion rewrite warning messages filterable #13429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 changelog/5473.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace `:` with `;` in the assertion rewrite warning message so it can be filtered using standard Python warning filters before calling :func:`pytest.main`.
2 changes: 1 addition & 1 deletion src/_pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def _warn_already_imported(self, name: str) -> None:

self.config.issue_config_time_warning(
PytestAssertRewriteWarning(
f"Module already imported so cannot be rewritten: {name}"
f"Module already imported so cannot be rewritten; {name}"
),
stacklevel=5,
)
Expand Down
18 changes: 17 additions & 1 deletion testing/test_assertrewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,23 @@ def test_rewrite_warning(self, pytester: Pytester) -> None:
)
# needs to be a subprocess because pytester explicitly disables this warning
result = pytester.runpytest_subprocess()
result.stdout.fnmatch_lines(["*Module already imported*: _pytest"])
result.stdout.fnmatch_lines(["*Module already imported*; _pytest"])

def test_rewrite_warning_ignore(self, pytester: Pytester) -> None:
pytester.makeconftest(
"""
import pytest
pytest.register_assert_rewrite("_pytest")
"""
)
# needs to be a subprocess because pytester explicitly disables this warning
result = pytester.runpytest_subprocess(
"-W",
"ignore:Module already imported so cannot be rewritten; _pytest:pytest.PytestAssertRewriteWarning",
)
# Previously, when the message pattern used to contain an extra `:`, an error was raised.
assert not result.stderr.str().strip()
result.stdout.no_fnmatch_line("*Module already imported*; _pytest")

def test_rewrite_module_imported_from_conftest(self, pytester: Pytester) -> None:
pytester.makeconftest(
Expand Down