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

requests_mock.Mocker decorator does not work on async tests and also breaks subsequent (higher) patch decorators #236

Open
dhausauer-flx opened this issue Jul 12, 2023 · 0 comments

Comments

@dhausauer-flx
Copy link

dhausauer-flx commented Jul 12, 2023

It appears that applying a requests_mock.Mocker decorator on an async test function of a unittest.IsolatedAsyncioTestCase does not work:

    @requests_mock.Mocker()
    async def test_mocker(self, m):
        m.get("http://127.0.0.1", text="response")
        requests.get("http://127.0.0.1", timeout=0.5)  # error
        assert m.called()  # not reached

Furthermore, it appears that when the requests_mock.Mocker decorator is applied after any unittest.mock.patch decorators, the preceding patch decorators are disabled:

    @mock.patch("test_requests_mock.should_not_run", return_value=True)
    @requests_mock.Mocker()
    async def test_patch_before_mocker(self, *_):
        assert should_not_run()  # fails

This does not occur if the requests_mock.Mocker decorator is placed above the unittest.mock.patch decorators:

    @requests_mock.Mocker()
    @mock.patch("test_requests_mock.should_not_run", return_value=True)
    async def test_patch_after_mocker(self, *_):
        assert should_not_run()  # passes

Tested with requests-mock version 1.11.0 on Python 3.7.9.

Full test file is below:

# test_requests_mock.py

# requirements.txt
# requests==2.31.0
# requests-mock==1.11.0

from unittest import IsolatedAsyncioTestCase, mock

import requests
import requests_mock


def should_not_run():
    return False


class TestRequestsMock(IsolatedAsyncioTestCase):
    @requests_mock.Mocker()
    async def test_mocker(self, m):
        m.get("http://127.0.0.1", text="response")
        requests.get("http://127.0.0.1", timeout=0.5)  # error
        assert m.called()  # not reached

    @mock.patch("test_requests_mock.should_not_run", return_value=True)
    @requests_mock.Mocker()
    async def test_patch_before_mocker(self, *_):
        assert should_not_run()  # fails

    @requests_mock.Mocker()
    @mock.patch("test_requests_mock.should_not_run", return_value=True)
    async def test_patch_after_mocker(self, *_):
        assert should_not_run()  # passes
@dhausauer-flx dhausauer-flx changed the title requests_mock.Mocker decorator does not work on async tests and also breaks previous patch decorators requests_mock.Mocker decorator does not work on async tests and also breaks subsequent (higher) patch decorators Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant