|
3 | 3 | from typing import List
|
4 | 4 | from typing import Optional
|
5 | 5 | from typing import Type
|
| 6 | +from typing import Union |
6 | 7 | import warnings
|
7 | 8 |
|
8 | 9 | import pytest
|
@@ -546,24 +547,34 @@ def test_it():
|
546 | 547 | result.assert_outcomes()
|
547 | 548 |
|
548 | 549 |
|
549 |
| -def test_raise_type_error_on_non_string_warning() -> None: |
550 |
| - """Check pytest.warns validates warning messages are strings (#10865).""" |
551 |
| - with pytest.raises(TypeError, match="Warning message must be str"): |
| 550 | +def test_raise_type_error_on_invalid_warning() -> None: |
| 551 | + """Check pytest.warns validates warning messages are strings (#10865) or |
| 552 | + Warning instances (#11959).""" |
| 553 | + with pytest.raises(TypeError, match="Warning must be str or Warning"): |
552 | 554 | with pytest.warns(UserWarning):
|
553 | 555 | warnings.warn(1) # type: ignore
|
554 | 556 |
|
555 | 557 |
|
556 |
| -def test_no_raise_type_error_on_string_warning() -> None: |
557 |
| - """Check pytest.warns validates warning messages are strings (#10865).""" |
558 |
| - with pytest.warns(UserWarning): |
559 |
| - warnings.warn("Warning") |
| 558 | +@pytest.mark.parametrize( |
| 559 | + "message", |
| 560 | + [ |
| 561 | + pytest.param("Warning", id="str"), |
| 562 | + pytest.param(UserWarning(), id="UserWarning"), |
| 563 | + pytest.param(Warning(), id="Warning"), |
| 564 | + ], |
| 565 | +) |
| 566 | +def test_no_raise_type_error_on_valid_warning(message: Union[str, Warning]) -> None: |
| 567 | + """Check pytest.warns validates warning messages are strings (#10865) or |
| 568 | + Warning instances (#11959).""" |
| 569 | + with pytest.warns(Warning): |
| 570 | + warnings.warn(message) |
560 | 571 |
|
561 | 572 |
|
562 | 573 | @pytest.mark.skipif(
|
563 | 574 | hasattr(sys, "pypy_version_info"),
|
564 | 575 | reason="Not for pypy",
|
565 | 576 | )
|
566 |
| -def test_raise_type_error_on_non_string_warning_cpython() -> None: |
| 577 | +def test_raise_type_error_on_invalid_warning_message_cpython() -> None: |
567 | 578 | # Check that we get the same behavior with the stdlib, at least if filtering
|
568 | 579 | # (see https://github.com/python/cpython/issues/103577 for details)
|
569 | 580 | with pytest.raises(TypeError):
|
|
0 commit comments