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

Export names matchers and Response from RequestsMock instances #739

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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