Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lundberg/respx
Mock HTTPX with awesome request patterns and response side effects 🦋
https://github.com/lundberg/respx
httpx mock pytest testing
Last synced: 11 days ago
JSON representation
Mock HTTPX with awesome request patterns and response side effects 🦋
- Host: GitHub
- URL: https://github.com/lundberg/respx
- Owner: lundberg
- License: bsd-3-clause
- Created: 2019-11-13T15:57:09.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-16T13:42:24.000Z (7 months ago)
- Last Synced: 2024-10-31T09:38:53.025Z (11 days ago)
- Topics: httpx, mock, pytest, testing
- Language: Python
- Homepage: https://lundberg.github.io/respx
- Size: 1.32 MB
- Stars: 603
- Watchers: 4
- Forks: 39
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
RESPX - Mock HTTPX with awesome request patterns and response side effects.---
[![tests](https://img.shields.io/github/actions/workflow/status/lundberg/respx/test.yml?branch=master&label=tests&logo=github&logoColor=white&style=for-the-badge)](https://github.com/lundberg/respx/actions/workflows/test.yml)
[![codecov](https://img.shields.io/codecov/c/github/lundberg/respx?logo=codecov&logoColor=white&style=for-the-badge)](https://codecov.io/gh/lundberg/respx)
[![PyPi Version](https://img.shields.io/pypi/v/respx?logo=pypi&logoColor=white&style=for-the-badge)](https://pypi.org/project/respx/)
[![Python Versions](https://img.shields.io/pypi/pyversions/respx?logo=python&logoColor=white&style=for-the-badge)](https://pypi.org/project/respx/)## Documentation
Full documentation is available at
[lundberg.github.io/respx](https://lundberg.github.io/respx/)## QuickStart
RESPX is a simple, _yet powerful_, utility for mocking out the
[HTTPX](https://www.python-httpx.org/), _and
[HTTP Core](https://www.encode.io/httpcore/)_, libraries.Start by [patching](https://lundberg.github.io/respx/guide/#mock-httpx) `HTTPX`, using
`respx.mock`, then add request
[routes](https://lundberg.github.io/respx/guide/#routing-requests) to mock
[responses](https://lundberg.github.io/respx/guide/#mocking-responses).```python
import httpx
import respxfrom httpx import Response
@respx.mock
def test_example():
my_route = respx.get("https://example.org/").mock(return_value=Response(204))
response = httpx.get("https://example.org/")
assert my_route.called
assert response.status_code == 204
```> Read the [User Guide](https://lundberg.github.io/respx/guide/) for a complete
> walk-through.### pytest + httpx
For a neater `pytest` experience, RESPX includes a `respx_mock` _fixture_ for easy
`HTTPX` mocking, along with an optional `respx` _marker_ to fine-tune the mock
[settings](https://lundberg.github.io/respx/api/#configuration).```python
import httpx
import pytestdef test_default(respx_mock):
respx_mock.get("https://foo.bar/").mock(return_value=httpx.Response(204))
response = httpx.get("https://foo.bar/")
assert response.status_code == 204@pytest.mark.respx(base_url="https://foo.bar")
def test_with_marker(respx_mock):
respx_mock.get("/baz/").mock(return_value=httpx.Response(204))
response = httpx.get("https://foo.bar/baz/")
assert response.status_code == 204
```## Installation
Install with pip:
```console
$ pip install respx
```Requires Python 3.7+ and HTTPX 0.21+. See
[Changelog](https://github.com/lundberg/respx/blob/master/CHANGELOG.md) for older HTTPX
compatibility.