| 289 | warnings.warn("w3", UserWarning) |
| 290 | |
| 291 | def test_as_contextmanager(self) -> None: |
| 292 | with pytest.warns(RuntimeWarning): |
| 293 | warnings.warn("runtime", RuntimeWarning) |
| 294 | |
| 295 | with pytest.warns(UserWarning): |
| 296 | warnings.warn("user", UserWarning) |
| 297 | |
| 298 | with pytest.warns(): |
| 299 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 300 | with pytest.warns(RuntimeWarning): |
| 301 | warnings.warn("user", UserWarning) |
| 302 | excinfo.match( |
| 303 | r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n" |
| 304 | r" Emitted warnings: \[UserWarning\('user',?\)\]." |
| 305 | ) |
| 306 | |
| 307 | with pytest.warns(): |
| 308 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 309 | with pytest.warns(UserWarning): |
| 310 | warnings.warn("runtime", RuntimeWarning) |
| 311 | excinfo.match( |
| 312 | r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n" |
| 313 | r" Emitted warnings: \[RuntimeWarning\('runtime',?\)]." |
| 314 | ) |
| 315 | |
| 316 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 317 | with pytest.warns(UserWarning): |
| 318 | pass |
| 319 | excinfo.match( |
| 320 | r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n" |
| 321 | r" Emitted warnings: \[\]." |
| 322 | ) |
| 323 | |
| 324 | warning_classes = (UserWarning, FutureWarning) |
| 325 | with pytest.warns(): |
| 326 | with pytest.raises(pytest.fail.Exception) as excinfo: |
| 327 | with pytest.warns(warning_classes) as warninfo: |
| 328 | warnings.warn("runtime", RuntimeWarning) |
| 329 | warnings.warn("import", ImportWarning) |
| 330 | |
| 331 | messages = [each.message for each in warninfo] |
| 332 | expected_str = ( |
| 333 | f"DID NOT WARN. No warnings of type {warning_classes} were emitted.\n" |
| 334 | f" Emitted warnings: {messages}." |
| 335 | ) |
| 336 | |
| 337 | assert str(excinfo.value) == expected_str |
| 338 | |
| 339 | def test_record(self) -> None: |
| 340 | with pytest.warns(UserWarning) as record: |