| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import inspect |
| 5 |
|
| 6 |
from _pytest import warning_types |
| 7 |
from _pytest.pytester import Pytester |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.parametrize( |
| 12 |
"warning_class", |
| 13 |
[ |
| 14 |
w |
| 15 |
for n, w in vars(warning_types).items() |
| 16 |
if inspect.isclass(w) and issubclass(w, Warning) |
| 17 |
], |
| 18 |
) |
| 19 |
def test_warning_types(warning_class: UserWarning) -> None: |
| 20 |
"""Make sure all warnings declared in _pytest.warning_types are displayed as coming |
| 21 |
from 'pytest' instead of the internal module (#5452). |
| 22 |
""" |
| 23 |
assert warning_class.__module__ == "pytest" |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.filterwarnings("error::pytest.PytestWarning") |
| 27 |
def test_pytest_warnings_repr_integration_test(pytester: Pytester) -> None: |
| 28 |
"""Small integration test to ensure our small hack of setting the __module__ attribute |
| 29 |
of our warnings actually works (#5452). |
| 30 |
""" |
| 31 |
pytester.makepyfile( |
| 32 |
""" |
| 33 |
import pytest |
| 34 |
import warnings |
| 35 |
|
| 36 |
def test(): |
| 37 |
warnings.warn(pytest.PytestWarning("some warning")) |
| 38 |
""" |
| 39 |
) |
| 40 |
result = pytester.runpytest() |
| 41 |
result.stdout.fnmatch_lines(["E pytest.PytestWarning: some warning"]) |
| 42 |
|
| 43 |
|
| 44 |
@pytest.mark.filterwarnings("error") |
| 45 |
def test_warn_explicit_for_annotates_errors_with_location(): |
| 46 |
with pytest.raises(Warning, match=r"(?m)test\n at .*raises.py:\d+"): |
| 47 |
warning_types.warn_explicit_for( |
| 48 |
pytest.raises, # type: ignore[arg-type] |
| 49 |
warning_types.PytestWarning("test"), |
| 50 |
) |