I have an existing context manager that is required by multiple tests. Instead of writing a with block inside each test, I thought it would be better to make a fixture out of this context manager, and decorate the test with @pytest.mark.usefixtures("my_fixture").
I could reimplement the context manager as a fixture, but this seems like duplicating code. So I'd like to reference the original context manager in the new fixture.
This is what I have:
import my_context_manager
@pytest.fixture
def my_fixture(arg1, arg2):
with my_context_manager(arg1, arg2) as c:
yield c
Is this an appropriate way to convert an existing context manager into a fixture?
I should mention that I'm aware of contextlib.ContextDecorator for writing a context manager that can be used as a decorator. But my context manager requires arguments, and these are not recognized when they are in a statement like @my_context_decorator(arg1, arg2).