Skip to content

Commit c796564

Browse files
authored
Export names matchers and Response from RequestsMock instances (#739)
* 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
1 parent f1055f4 commit c796564

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

README.rst

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -899,16 +899,19 @@ Integration with unit test frameworks
899899
Responses as a ``pytest`` fixture
900900
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
901901

902+
Use the pytest-responses package to export ``responses`` as a pytest fixture.
903+
904+
``pip install pytest-responses``
905+
906+
You can then access it in a pytest script using:
907+
902908
.. code-block:: python
903909
904-
@pytest.fixture
905-
def mocked_responses():
906-
with responses.RequestsMock() as rsps:
907-
yield rsps
910+
import pytest_responses
908911
909912
910-
def test_api(mocked_responses):
911-
mocked_responses.get(
913+
def test_api(responses):
914+
responses.get(
912915
"http://twitter.com/api/1/foobar",
913916
body="{}",
914917
status=200,

responses/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@ class RequestsMock:
714714
POST: Literal["POST"] = "POST"
715715
PUT: Literal["PUT"] = "PUT"
716716

717+
Response: Type[Response] = Response
718+
719+
# Make the `matchers` name available under a RequestsMock instance
720+
from responses import matchers
721+
717722
response_callback: Optional[Callable[[Any], Any]] = None
718723

719724
def __init__(

responses/tests/test_matchers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,29 @@ def run():
844844
assert_reset()
845845

846846

847+
def test_matchers_under_requests_mock_object():
848+
def run():
849+
# ensure all access to responses or matchers is only going
850+
# through the RequestsMock instance in the context manager
851+
responses = None # noqa: F841
852+
matchers = None # noqa: F841
853+
from responses import RequestsMock
854+
855+
with RequestsMock(assert_all_requests_are_fired=True) as rsps:
856+
url = "http://example.com"
857+
rsps.add(
858+
rsps.GET,
859+
url,
860+
body=b"test",
861+
match=[rsps.matchers.body_matcher("123456")],
862+
)
863+
resp = requests.get("http://example.com", data="123456")
864+
assert_response(resp, "test")
865+
866+
run()
867+
assert_reset()
868+
869+
847870
class TestHeaderWithRegex:
848871
@property
849872
def url(self): # type: ignore[misc]

responses/tests/test_responses.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ def run():
108108
assert_reset()
109109

110110

111+
def test_response_with_instance_under_requests_mock_object():
112+
def run():
113+
# ensure all access to responses is only going through
114+
# the RequestsMock instance in the context manager
115+
responses = None # noqa: F841
116+
from responses import RequestsMock
117+
118+
with RequestsMock(assert_all_requests_are_fired=True) as rsps:
119+
rsps.add(rsps.Response(method=rsps.GET, url="http://example.com"))
120+
resp = requests.get("http://example.com")
121+
assert_response(resp, "")
122+
assert len(rsps.calls) == 1
123+
assert rsps.calls[0].request.url == "http://example.com/"
124+
125+
resp = requests.get("http://example.com?foo=bar")
126+
assert_response(resp, "")
127+
assert len(rsps.calls) == 2
128+
assert rsps.calls[1].request.url == "http://example.com/?foo=bar"
129+
130+
run()
131+
assert_reset()
132+
133+
111134
@pytest.mark.parametrize(
112135
"original,replacement",
113136
[

0 commit comments

Comments
 (0)