|
1 | 1 | import warnings
|
| 2 | +from typing import List |
2 | 3 | from typing import Optional
|
| 4 | +from typing import Type |
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 | from _pytest.pytester import Pytester
|
@@ -44,22 +46,37 @@ class ParentWarning(Warning):
|
44 | 46 | class ChildWarning(ParentWarning):
|
45 | 47 | pass
|
46 | 48 |
|
47 |
| - class RandomWarning(Warning): |
| 49 | + class ChildOfChildWarning(ChildWarning): |
48 | 50 | pass
|
49 | 51 |
|
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) |
54 | 56 |
|
55 | 57 | def test_pop(self):
|
56 | 58 | 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 | + ) |
58 | 62 |
|
59 |
| - assert len(record) == 2 |
| 63 | + assert len(record) == 3 |
60 | 64 | _warn = record.pop(self.ParentWarning)
|
61 | 65 | assert _warn.category is self.ParentWarning
|
62 | 66 |
|
| 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 | + |
63 | 80 |
|
64 | 81 | class TestWarningsRecorderChecker:
|
65 | 82 | def test_recording(self) -> None:
|
|
0 commit comments