Skip to content

Commit 3164062

Browse files
authored
Merge pull request #2150 from lesteve/add-caught-warnings-info-when-warns-fail
Improve error message when pytest.warns fail
2 parents a27c824 + bfada96 commit 3164062

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Katarzyna Jachim
8282
Kevin Cox
8383
Lee Kamentsky
8484
Lev Maximov
85+
Loic Esteve
8586
Lukas Bednar
8687
Luke Murphy
8788
Maciek Fijalkowski

CHANGELOG.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55

66
*
77

8-
*
8+
* Improve error message when pytest.warns fails (`#2150`_). The type(s) of the
9+
expected warnings and the list of caught warnings is added to the
10+
error message. Thanks `@lesteve`_ for the PR.
911

1012
*
1113

1214
*
1315

16+
.. _@lesteve: https://github.com/lesteve
17+
18+
.. _#2150: https://github.com/pytest-dev/pytest/issues/2150
1419

1520

1621
3.0.5 (2016-12-05)

_pytest/recwarn.py

+4-1
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

+28-5
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)