question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passthrus persist accross tests

See original GitHub issue

Hello, 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:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
knaperekcommented, Apr 7, 2021

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?

1reaction
iot-resistercommented, Apr 7, 2021

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enable Passthrough for a Network Device on a Host
Browse to a host in the vSphere Client navigator. · On the Configure tab, expand Hardware and click PCI Devices. · To enable...
Read more >
Quest2 passthrough reflections testing - YouTube
Testing out a new technique for masked reflections on Quest2 passthrough.
Read more >
TESTING FOR COMPLETE PASS-THROUGH OF EX ASS ...
The objective of this paper is to test whether the complete pass-through of exchange rate exists when there are almost no transaction costs...
Read more >
K9347: Configuring passthrough FTPS load balancing - AskF5
To configure the persistence profile, perform the following procedure: Log in to the Configuration utility. Select Local Traffic. Select ...
Read more >
Virtual GPU Software User Guide
Licensing settings persist across reboots and need only be modified if the license server address changes, or the VM is switched to running...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found