|
1 | 1 | import warnings
|
| 2 | +import re |
2 | 3 | import py
|
3 | 4 | import pytest
|
4 | 5 | from _pytest.recwarn import WarningsRecorder
|
@@ -114,7 +115,7 @@ def test_deprecated_call_as_context_manager_no_warning(self):
|
114 | 115 | with pytest.raises(pytest.fail.Exception) as ex:
|
115 | 116 | with pytest.deprecated_call():
|
116 | 117 | self.dep(1)
|
117 |
| - assert str(ex.value) == "DID NOT WARN" |
| 118 | + assert str(ex.value).startswith("DID NOT WARN") |
118 | 119 |
|
119 | 120 | def test_deprecated_call_as_context_manager(self):
|
120 | 121 | with pytest.deprecated_call():
|
@@ -185,16 +186,38 @@ def test_as_contextmanager(self):
|
185 | 186 | with pytest.warns(RuntimeWarning):
|
186 | 187 | warnings.warn("runtime", RuntimeWarning)
|
187 | 188 |
|
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: |
189 | 193 | with pytest.warns(RuntimeWarning):
|
190 | 194 | 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',\)\].") |
191 | 197 |
|
192 |
| - with pytest.raises(pytest.fail.Exception): |
| 198 | + with pytest.raises(pytest.fail.Exception) as excinfo: |
193 | 199 | with pytest.warns(UserWarning):
|
194 | 200 | 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]))) |
195 | 220 |
|
196 |
| - with pytest.warns(UserWarning): |
197 |
| - warnings.warn("user", UserWarning) |
198 | 221 |
|
199 | 222 | def test_record(self):
|
200 | 223 | with pytest.warns(UserWarning) as record:
|
|
0 commit comments