Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behaviour of assert_all_requests_are_fired between decorator and context manager #726

Open
andrew-cybsafe opened this issue Jul 4, 2024 · 2 comments
Assignees

Comments

@andrew-cybsafe
Copy link

Describe the bug

When a RequestsMock() is used as a context manager, assert_all_requests_are_fired is defaulted to True and an exception is raised at the end of the context block if a response is unused. However, using the @responses.activate decorator on a test function defaults assert_all_requests_are_fired to False leading to inconsistent behaviour.

Additional context

As a workaround, I have tried setting responses.mock.assert_all_requests_are_fired = True in conftest.py or just after an import of requests. However, this does not change the behaviour. The issue appears to be that the value in the default mock is never used as it gets defaulted by a default parameter in activate(), see: code.

Version of responses

0.25.3

Steps to Reproduce

import responses
import requests


def test_my_api():
    with responses.RequestsMock() as rsps:
        rsps.add(
            responses.GET,
            "http://twitter.com/api/1/foobar",
            body="{}",
            status=200,
            content_type="application/json",
        )


@responses.activate
def test_my_api_2():
    responses.add(
        responses.GET,
        "http://twitter.com/api/1/foobar",
        body="{}",
        status=200,
        content_type="application/json",
    )

Expected Result

Both test cases to fail with:

AssertionError: Not all requests have been executed [('GET', 'http://twitter.com/api/1/foobar')]

Actual Result

First test case fails.
Second test case passes.

@markstory
Copy link
Member

Changing the default values is not something we can easily do at this point in time as it will break existing assumptions in userland code.

I have tried setting responses.mock.assert_all_requests_are_fired = True in conftest.py or just after an import of requests.

Because of fixes we had to make to fix nested scopes, I don't think this is currently working. But it should be possible to have it work.

@andrew-cybsafe
Copy link
Author

andrew-cybsafe commented Jul 16, 2024

@markstory I've opened PR #731 for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

3 participants