Skip to main content

pytest-responsemock

https://github.com/idlesign/pytest-responsemock

release lic coverage

Description

Simplified HTTP calls mocking for pytest (requests, httpx)

Provides response_mock fixture, exposing simple context manager.

Any request under that manager will be intercepted and mocked according to one or more rules passed to the manager. If actual request won't fall under any of given rules then an exception is raised (by default).

Rules are simple strings, of the pattern: HTTP_METHOD URL -> STATUS_CODE :BODY.

Requests made with both requests and httpx (sync and async clients) are intercepted. Use the backend manager argument (requests/httpx/all) to select the backend, or the aresponse_mock fixture as an httpx-oriented shortcut.

Requirements

  • Python 3.10+

Usage

When this package is installed response_mock is available for pytest test functions.

def for_test():
    return requests.get('http://some.domain')


def test_me(response_mock):

    # Pass response rule as a string,
    # or many rules (to mock consequent requests) as a list of strings/bytes.
    # Use optional `bypass` argument to disable mock conditionally.

    with response_mock('GET http://some.domain -> 200 :Nice', bypass=False):

        result = for_test()

        assert result.ok
        assert result.content == b'Nice'

    # mock consequent requests
    with response_mock([
        'GET http://some.domain -> 200 :Nice',
        'GET http://other.domain -> 200 :Sweet',
    ]):
        for_test()
        requests.get('http://other.domain')

Use with pytest-datafixtures:

def test_me(response_mock, datafix_read):

    with response_mock(f"GET http://some.domain -> 200 :{datafix_read('myresponse.html')}"):
        ...

Describe response header fields using multiline strings:

with response_mock(
    '''
    GET http://some.domain

    Allow: GET, HEAD
    Content-Language: ru

    -> 200 :OK
    '''
):
    ...

Test json response:

response = json.dumps({'key': 'value', 'another': 'yes'})

with response_mock(f'POST http://some.domain -> 400 :{response}'):
    ...

To test binary response pass rule as bytes:

with response_mock(b'GET http://some.domain -> 200 :' + my_bytes):
    ...

Access underlying RequestsMock (from responses package) as mock:

with response_mock('HEAD http://some.domain -> 200 :Nope') as mock:

    mock.add_passthru('http://other.domain')

Metadata

Release files for pytest-responsemock 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-responsemock 2.0.0
File Size Uploaded Provenance Actions
pytest_responsemock-2.0.0.tar.gz 5.3 kB Without provenance Details for pytest_responsemock-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-responsemock 2.0.0
File Interpreter ABI Platform Actions
pytest_responsemock-2.0.0-py3-none-any.whl Python 3 none any Details for pytest_responsemock-2.0.0-py3-none-any.whl

Total release size: 12.2 kB

Release files / pytest_responsemock-2.0.0.tar.gz

Download URL pytest_responsemock-2.0.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e9c4fee31de62e750b6be0b41c3c1d21356e50ad506ab27da5fae347e5a00cee
BLAKE2b-256 checksum
How to use checksums
5c1a663236f27b726a964397c6c94bb4cb1b8654f42ba209b1469f605ca95570
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pytest_responsemock-2.0.0-py3-none-any.whl

Download URL pytest_responsemock-2.0.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edbba1c7badb866d0759e9f89b9466be65e1f08df6750fd9cc805e7a371052d3
BLAKE2b-256 checksum
How to use checksums
eac41d25abc9d8ad3a222c83814e93e072fd549aa20a76fc5da140f55a708b4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for pytest-responsemock 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page