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