Skip to content

Commit c5f0b75

Browse files
committed
Improve error message when pytest.warns fail
The error message contains the expected type of warnings and the warnings that were captured. Add tests.
1 parent da40bcf commit c5f0b75

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

_pytest/recwarn.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,7 @@ def __exit__(self, *exc_info):
223223
if self.expected_warning is not None:
224224
if not any(r.category in self.expected_warning for r in self):
225225
__tracebackhide__ = True
226-
pytest.fail("DID NOT WARN")
226+
pytest.fail("DID NOT WARN. No warnings of type {0} was emitted. "
227+
"The list of emitted warnings is: {1}.".format(
228+
self.expected_warning,
229+
[each.message for each in self]))

testing/test_recwarn.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import warnings
2+
import re
23
import py
34
import pytest
45
from _pytest.recwarn import WarningsRecorder
@@ -114,7 +115,7 @@ def test_deprecated_call_as_context_manager_no_warning(self):
114115
with pytest.raises(pytest.fail.Exception) as ex:
115116
with pytest.deprecated_call():
116117
self.dep(1)
117-
assert str(ex.value) == "DID NOT WARN"
118+
assert str(ex.value).startswith("DID NOT WARN")
118119

119120
def test_deprecated_call_as_context_manager(self):
120121
with pytest.deprecated_call():
@@ -185,16 +186,38 @@ def test_as_contextmanager(self):
185186
with pytest.warns(RuntimeWarning):
186187
warnings.warn("runtime", RuntimeWarning)
187188

188-
with pytest.raises(pytest.fail.Exception):
189+
with pytest.warns(UserWarning):
190+
warnings.warn("user", UserWarning)
191+
192+
with pytest.raises(pytest.fail.Exception) as excinfo:
189193
with pytest.warns(RuntimeWarning):
190194
warnings.warn("user", UserWarning)
195+
excinfo.match(r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) was emitted. "
196+
r"The list of emitted warnings is: \[UserWarning\('user',\)\].")
191197

192-
with pytest.raises(pytest.fail.Exception):
198+
with pytest.raises(pytest.fail.Exception) as excinfo:
193199
with pytest.warns(UserWarning):
194200
warnings.warn("runtime", RuntimeWarning)
201+
excinfo.match(r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
202+
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',\)\].")
203+
204+
with pytest.raises(pytest.fail.Exception) as excinfo:
205+
with pytest.warns(UserWarning):
206+
pass
207+
excinfo.match(r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) was emitted. "
208+
r"The list of emitted warnings is: \[\].")
209+
210+
warning_classes = (UserWarning, FutureWarning)
211+
with pytest.raises(pytest.fail.Exception) as excinfo:
212+
with pytest.warns(warning_classes) as warninfo:
213+
warnings.warn("runtime", RuntimeWarning)
214+
warnings.warn("import", ImportWarning)
215+
216+
message_template = ("DID NOT WARN. No warnings of type {0} was emitted. "
217+
"The list of emitted warnings is: {1}.")
218+
excinfo.match(re.escape(message_template.format(warning_classes,
219+
[each.message for each in warninfo])))
195220

196-
with pytest.warns(UserWarning):
197-
warnings.warn("user", UserWarning)
198221

199222
def test_record(self):
200223
with pytest.warns(UserWarning) as record:

0 commit comments

Comments
 (0)