Skip to content

Commit

Permalink
Export names matchers and Response from RequestsMock instances (#739
Browse files Browse the repository at this point in the history
)

* Export names `matchers` and `Response` from RequestsMock instances

This makes it possible to import or access only RequestsMock (possibly via a pytest fixture) and still be able to easily access the matchers or assemble a Response object.

It also makes it easier to adopt pytest-responses which uses `responses` as the fixture variable which would conflict with an import of this module.

Add test cases that mask the existing imports and still access matchers or assemble a Response object via the RequestsMock instance.

* Recommend using pytest-responses for a pytest fixture

Rather than explaining how to roll your own in Markdown, recommend using the companion package to get a standard `responses` pytest fixture easily usable in test cases
  • Loading branch information
filbranden authored Oct 2, 2024
1 parent f1055f4 commit c796564
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
15 changes: 9 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -899,16 +899,19 @@ Integration with unit test frameworks
Responses as a ``pytest`` fixture
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Use the pytest-responses package to export ``responses`` as a pytest fixture.

``pip install pytest-responses``

You can then access it in a pytest script using:

.. code-block:: python
@pytest.fixture
def mocked_responses():
with responses.RequestsMock() as rsps:
yield rsps
import pytest_responses
def test_api(mocked_responses):
mocked_responses.get(
def test_api(responses):
responses.get(
"http://twitter.com/api/1/foobar",
body="{}",
status=200,
Expand Down
5 changes: 5 additions & 0 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@ class RequestsMock:
POST: Literal["POST"] = "POST"
PUT: Literal["PUT"] = "PUT"

Response: Type[Response] = Response

# Make the `matchers` name available under a RequestsMock instance
from responses import matchers

response_callback: Optional[Callable[[Any], Any]] = None

def __init__(
Expand Down
23 changes: 23 additions & 0 deletions responses/tests/test_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,29 @@ def run():
assert_reset()


def test_matchers_under_requests_mock_object():
def run():
# ensure all access to responses or matchers is only going
# through the RequestsMock instance in the context manager
responses = None # noqa: F841
matchers = None # noqa: F841
from responses import RequestsMock

with RequestsMock(assert_all_requests_are_fired=True) as rsps:
url = "http://example.com"
rsps.add(
rsps.GET,
url,
body=b"test",
match=[rsps.matchers.body_matcher("123456")],
)
resp = requests.get("http://example.com", data="123456")
assert_response(resp, "test")

run()
assert_reset()


class TestHeaderWithRegex:
@property
def url(self): # type: ignore[misc]
Expand Down
23 changes: 23 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ def run():
assert_reset()


def test_response_with_instance_under_requests_mock_object():
def run():
# ensure all access to responses is only going through
# the RequestsMock instance in the context manager
responses = None # noqa: F841
from responses import RequestsMock

with RequestsMock(assert_all_requests_are_fired=True) as rsps:
rsps.add(rsps.Response(method=rsps.GET, url="http://example.com"))
resp = requests.get("http://example.com")
assert_response(resp, "")
assert len(rsps.calls) == 1
assert rsps.calls[0].request.url == "http://example.com/"

resp = requests.get("http://example.com?foo=bar")
assert_response(resp, "")
assert len(rsps.calls) == 2
assert rsps.calls[1].request.url == "http://example.com/?foo=bar"

run()
assert_reset()


@pytest.mark.parametrize(
"original,replacement",
[
Expand Down

0 comments on commit c796564

Please sign in to comment.