For pytest >= 7.0
The doc now explicitely mentions this case should be solved this way (without pytest):
with warnings.catch_warnings():
warnings.simplefilter("error")
...
though this may not completely solve some cases (dynamic checks: see this post).
The solution suggested for pytest < 7.0, below, now raises a DeprecationWarning. Thanks to @Warren-Weckesser for signaling this in comment!
Possible solution for pytest < 7.0
Yet it was not planned to be used like this, it’s possible to “record” any possible warning raised, and use this to add another assertion to ensure the number of raised warnings is 0:
def test_AttrStr_parse_warnings():
"""Check parse() raises proper warnings in proper cases."""
with pytest.warns(None) as record:
_AttrStr('').parse()
assert len(record) == 0
To ensure it works: adding warnings.warn('any message') in the second assertion let the test fail.