You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fixture Creates Second Mock, Cleans up Only One #22
Responses is not active in the second test. Both tests pass.
Actual Result
Responses is still active and raises requests.exceptions.ConnectionError: Connection refused by Responses - the call doesn't match any registered mock.
Possible Explanation
Responses already creates a default RequestsMock. The fixture creates another mock, but the teardown will only cleanup one mock.
Instead of creating a second mock, the fixture could just return the already created default mock, like this:
I think a much cleaner solution actually is to drop the pytest_runtest_setup(), pytest_runtest_teardown() and the @pytest.mark.withoutresponses altogether and simply provide the responses fixture, so a test that wants to enable responses simply has to include the fixture, and any test that doesn't will not be affected by it.
Though it would be nice to still have a standard pytest fixture exported under the responses name, without having to define it ad hoc on any pytest script that wants to use it.
Activity
filbranden commentedon Sep 13, 2024
Indeed that's the case.
I think a much cleaner solution actually is to drop the
pytest_runtest_setup(),pytest_runtest_teardown()and the@pytest.mark.withoutresponsesaltogether and simply provide theresponsesfixture, so a test that wants to enableresponsessimply has to include the fixture, and any test that doesn't will not be affected by it.That makes the code almost trivial, just a 3-liner, in fact basically what's suggested under https://github.com/getsentry/responses/blob/master/README.rst#responses-as-a-pytest-fixture
Though it would be nice to still have a standard pytest fixture exported under the
responsesname, without having to define it ad hoc on any pytest script that wants to use it.