Skip to content

Commit 3d0dedb

Browse files
committed
test(warnings-recorder): Add attribute error test
1 parent 7b7bd30 commit 3d0dedb

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

testing/test_recwarn.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import warnings
2+
from typing import List
23
from typing import Optional
4+
from typing import Type
35

46
import pytest
57
from _pytest.pytester import Pytester
@@ -44,22 +46,37 @@ class ParentWarning(Warning):
4446
class ChildWarning(ParentWarning):
4547
pass
4648

47-
class RandomWarning(Warning):
49+
class ChildOfChildWarning(ChildWarning):
4850
pass
4951

50-
def raise_warnings(self):
51-
warnings.warn("Warning Random", self.RandomWarning)
52-
warnings.warn("Warning Child", self.ChildWarning)
53-
warnings.warn("Warning Parent", self.ParentWarning)
52+
@staticmethod
53+
def raise_warnings_from_list(_warnings: List[Type[Warning]]):
54+
for warn in _warnings:
55+
warnings.warn(f"Warning {warn().__repr__()}", warn)
5456

5557
def test_pop(self):
5658
with pytest.warns((self.ParentWarning, self.ChildWarning)) as record:
57-
self.raise_warnings()
59+
self.raise_warnings_from_list(
60+
[self.ChildWarning, self.ParentWarning, self.ChildOfChildWarning]
61+
)
5862

59-
assert len(record) == 2
63+
assert len(record) == 3
6064
_warn = record.pop(self.ParentWarning)
6165
assert _warn.category is self.ParentWarning
6266

67+
def test_pop_raises(self):
68+
with pytest.raises(AssertionError):
69+
with pytest.warns(self.ParentWarning) as record:
70+
self.raise_warnings_from_list([self.ParentWarning])
71+
record.pop(self.ChildOfChildWarning)
72+
73+
def test_pop_most_recent(self):
74+
with pytest.warns(self.ParentWarning) as record:
75+
self.raise_warnings_from_list([self.ChildWarning, self.ChildOfChildWarning])
76+
77+
_warn = record.pop(self.ParentWarning)
78+
assert _warn.category is self.ChildOfChildWarning
79+
6380

6481
class TestWarningsRecorderChecker:
6582
def test_recording(self) -> None:

0 commit comments

Comments
 (0)