| 253 | ) |
| 254 | |
| 255 | def test_as_contextmanager(self) -> None: |
| 256 | with pytest.warns(RuntimeWarning): |
| 257 | warnings.warn("runtime", RuntimeWarning) |
| 258 | |
| 259 | with pytest.warns(UserWarning): |
| 260 | warnings.warn("user", UserWarning) |
| 261 | |
| 262 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 263 | with pytest.warns(RuntimeWarning): |
| 264 | warnings.warn("user", UserWarning) |
| 265 | excinfo.match( |
| 266 | r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted. " |
| 267 | r"The list of emitted warnings is: \[UserWarning\('user',?\)\]." |
| 268 | ) |
| 269 | |
| 270 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 271 | with pytest.warns(UserWarning): |
| 272 | warnings.warn("runtime", RuntimeWarning) |
| 273 | excinfo.match( |
| 274 | r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted. " |
| 275 | r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]." |
| 276 | ) |
| 277 | |
| 278 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 279 | with pytest.warns(UserWarning): |
| 280 | pass |
| 281 | excinfo.match( |
| 282 | r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted. " |
| 283 | r"The list of emitted warnings is: \[\]." |
| 284 | ) |
| 285 | |
| 286 | warning_classes = (UserWarning, FutureWarning) |
| 287 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 288 | with pytest.warns(warning_classes) as warninfo: |
| 289 | warnings.warn("runtime", RuntimeWarning) |
| 290 | warnings.warn("import", ImportWarning) |
| 291 | |
| 292 | message_template = ( |
| 293 | "DID NOT WARN. No warnings of type {0} were emitted. " |
| 294 | "The list of emitted warnings is: {1}." |
| 295 | ) |
| 296 | excinfo.match( |
| 297 | re.escape( |
| 298 | message_template.format( |
| 299 | warning_classes, [each.message for each in warninfo] |
| 300 | ) |
| 301 | ) |
| 302 | ) |
| 303 | |
| 304 | def test_record(self) -> None: |
| 305 | with pytest.warns(UserWarning) as record: |