Fix checking expected warnings when an exception is raised
pytest.warn and similar functions should capture warnings when an exception is raised inside a with block
This commit is contained in:
parent
58cf20edf0
commit
6a8946718d
|
@ -0,0 +1 @@
|
||||||
|
``pytest.warns`` and similar functions now capture warnings when an exception is raised inside a ``with`` block.
|
|
@ -280,8 +280,6 @@ class WarningsChecker(WarningsRecorder):
|
||||||
def found_str():
|
def found_str():
|
||||||
return pformat([record.message for record in self], indent=2)
|
return pformat([record.message for record in self], indent=2)
|
||||||
|
|
||||||
# only check if we're not currently handling an exception
|
|
||||||
if exc_type is None and exc_val is None and exc_tb is None:
|
|
||||||
if self.expected_warning is not None:
|
if self.expected_warning is not None:
|
||||||
if not any(issubclass(r.category, self.expected_warning) for r in self):
|
if not any(issubclass(r.category, self.expected_warning) for r in self):
|
||||||
__tracebackhide__ = True
|
__tracebackhide__ = True
|
||||||
|
|
|
@ -179,6 +179,7 @@ class TestDeprecatedCall:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def f():
|
def f():
|
||||||
|
warnings.warn(DeprecationWarning("hi"))
|
||||||
raise ValueError("some exception")
|
raise ValueError("some exception")
|
||||||
|
|
||||||
with pytest.raises(ValueError, match="some exception"):
|
with pytest.raises(ValueError, match="some exception"):
|
||||||
|
|
Loading…
Reference in New Issue