pytest-responses
pytest-responses copied to clipboard
Separate responses startup/teardown from fixture
I'd like to use pytest_responses, but I don't like that simply including pytest_responses in the test suite enables responses for all my tests.
Although pytest_responses does provide a way to opt out of this behavior, I think it would be better to allow a project to opt in to this behavior, such that including pytest_responses
in the test environment has no default effect.
And instead, make the implicit startup/teardown explicit. e.g.:
@pytest.fixture
def all_responses(request):
if request.get_marker('withoutresponses'):
return
responses._start()
yield
# patcher was already uninstalled...
with contextlib.suppress(RuntimeError):
responses._stop()
responses._reset()
instead of the pytest_runtest_*
functions.
Then, a project that wishes to have this behavior could:
pytestmark = pytest.mark.usefixtures('all_responses')
In whichever scope they wish to enable responses (or in conftest.py for the global scope).
Right now, as implemented, it's not possible to get the responses
fixture without also getting the all_responses
behavior.
I haven't tested the above proposed technique, so there may be some issues that need to be resolved around it, but I was hoping to get some feedback on the concept, or learn if maybe there's something I'm missing that would otherwise alleviate my concern.