This would be analogous to PT012 which checks for pytest.raises contexts with multiple statements. If accepted, I volunteer to implement the check.
Description (adapted from the PT012 documentation)
Bad code:
import pytest
def test_foo():
with pytest.warns(UserWarning):
obj = get_object()
obj.do_something()
with pytest.warns(UserWarning):
if some_condition:
do_something()
Good code:
import pytest
def test_foo():
obj = get_object()
with pytest.warns(UserWarning):
obj.do_something()
An empty with-statement (containing only a pass inside) is allowed to allow testing of exceptions raised by context manager enter/exit methods.
import pytest
def test_my_context_manager():
context_manager = get_context_manager()
with pytest.warns(UserWarning):
with context_manager:
pass
Rationale
to help ensure that only the expected warning from code under test is caught in a pytest.warns block
Rule request
This would be analogous to
PT012
which checks forpytest.raises
contexts with multiple statements. If accepted, I volunteer to implement the check.Description (adapted from the
PT012
documentation)Bad code:
Good code:
An empty
with
-statement (containing only apass
inside) is allowed to allow testing of exceptions raised by context manager enter/exit methods.Rationale
pytest.warns
block