Skip to content

Commit 7183335

Browse files
dmitry.dygalonicoddemus
authored andcommitted
Capture warnings during pytest_configure
Fix #5115
1 parent a295a3d commit 7183335

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

changelog/5115.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Warnings issued during ``pytest_configure`` are explicitly not treated as errors, even if configured as such, because it otherwise completely breaks pytest.

src/_pytest/config/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,9 @@ def add_cleanup(self, func):
642642
def _do_configure(self):
643643
assert not self._configured
644644
self._configured = True
645-
self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
645+
with warnings.catch_warnings():
646+
warnings.simplefilter("default")
647+
self.hook.pytest_configure.call_historic(kwargs=dict(config=self))
646648

647649
def _ensure_unconfigure(self):
648650
if self._configured:

testing/test_warnings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,21 @@ def test_group_warnings_by_message(testdir):
645645
warning_code = 'warnings.warn(UserWarning("foo"))'
646646
assert warning_code in result.stdout.str()
647647
assert result.stdout.str().count(warning_code) == 1
648+
649+
650+
def test_pytest_configure_warning(testdir, recwarn):
651+
"""Issue 5115."""
652+
testdir.makeconftest(
653+
"""
654+
def pytest_configure():
655+
import warnings
656+
657+
warnings.warn("from pytest_configure")
658+
"""
659+
)
660+
661+
result = testdir.runpytest()
662+
assert result.ret == 5
663+
assert "INTERNALERROR" not in result.stderr.str()
664+
warning = recwarn.pop()
665+
assert str(warning.message) == "from pytest_configure"

0 commit comments

Comments
 (0)