Passthrus persist accross tests
See original GitHub issueHello, I just ran into what I believe is a bug. Calls to responses.add_passthru
persist across tests. This wouldn’t be a big deal except running tests in a random order causes failures.
This works:
import re
import responses
import requests
import pytest
@responses.activate
def test_without_a_passthru():
print(f"responses.passthru_prefixes: {responses.passthru_prefixes}")
print(f"responses._default_mock.passthru_prefixes: {responses._default_mock.passthru_prefixes}")
with pytest.raises(requests.exceptions.ConnectionError):
response = requests.get('https://example.com')
@responses.activate
def test_with_a_passthru():
print(f"responses.passthru_prefixes: {responses.passthru_prefixes}")
print(f"responses._default_mock.passthru_prefixes: {responses._default_mock.passthru_prefixes}")
responses.add_passthru(re.compile('.*'))
response = requests.get('https://example.com')
assert response.status_code == 200
This fails:
import re
import responses
import requests
import pytest
@responses.activate
def test_with_a_passthru():
print(f"responses.passthru_prefixes: {responses.passthru_prefixes}")
print(f"responses._default_mock.passthru_prefixes: {responses._default_mock.passthru_prefixes}")
responses.add_passthru(re.compile('.*'))
response = requests.get('https://example.com')
assert response.status_code == 200
@responses.activate
def test_without_a_passthru():
print(f"responses.passthru_prefixes: {responses.passthru_prefixes}")
print(f"responses._default_mock.passthru_prefixes: {responses._default_mock.passthru_prefixes}")
with pytest.raises(requests.exceptions.ConnectionError):
response = requests.get('https://example.com')
In the failing example you can see that since we called responses.add_passthru
in the first test case, the second test case fails because the passthru is still active.
Even though the passthru is still active, calling responses.passthru_prefixes
returns an empty tuple but calling responses._default_mock.passthru_prefixes
returns something different.
I think this is due to how it is assigned in the code. You are assigning a value, not a reference to _default_mock.passthru_prefixes
but that’s a quick guess without looking into the code too deep.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top GitHub Comments
The “unit” was intentionally enclosed with parenthesis, you can totally leave it out if it helps your understanding. Often it comes down to the definition of unit and your mileage may vary, nevertheless, the
unittest
standard Python library is not exclusively targeted for “unit” tests only and in fact it is very often used for integration tests as well (example: Django’s TestCase).The distinction between unit and integration (or other) types of tests is irrelevant in this context though. Test isolation is a very important aspect of any type of automated tests. The need to preserve state in an integration test only lasts until that test completes (successfully or not). You certainly don’t want that state to leak into other tests that are being executed in the same test run as a part of the same (or different) test suite.
The problem that I observed with this library is that fake responses defined for one test leak into other tests as well. That’s a bugger, because it breaks test isolation, which in turn breaks determinism and independence on the order of execution. I believe you want to have your integration test results independent on their order of execution, right?
@knaperek stop parroting unit testing insanity. This lib is obviously for integration testing. Which mean testing some state and state means no isolation. If you need this for your ‘unit’ tests, then you are doing somethign wrong. your unit tests should be isolated from any http stuff. its the D in SOLID.