Skip to main content

Pytest Catch Server

PyPI version Python versions Build status Code style: Black

Pytest plugin with server fixture for catching HTTP requests. It's handy for integration tests or for testing 3rd party packages which have network side effects like reporting to 3rd party services (DataDog APM, Sentry, ...).

Installation

You can install "pytest-catch-server" via pip:

pip install pytest-catch-server

Usage

This plugin comes with three fixtures. The main catch_server is for use in your tests:

def test_something(catch_server):
    url = f"http://{catch_server.host}:{catch_server.port}/something"
    requests.get(url)
    assert catch_server.requests == [
        {"method": "GET", "path": "/something", "data": b""}
    ]

In more real world examples you may need to patch global module (like some tracer).

It will flush list of catched requests between each test.

Catching these methods: GET, POST, PUT, PATCH, DELETE

You may want to setup your app fixture with catch server as dependency. You can use session scoped fixture background_catch_server for that:

@pytest.fixture(scope="session")
def app(background_catch_server):
    tracer_dsn = f"http://{background_catch_server.host}:{background_catch_server.port}"
    return create_app(tracer_dsn=tracer_dsn)

def test_tracing(app, catch_server):
    app.do_something_that_calls_tracer()
    assert catch_server.requests == [
        {"method": "PUT", "path": "/trace", "data": b"..."}
    ]

Port for catch server is a random free port. If you want to use specific port, you can override catch_server_port fixture in your tests which is returning free port number.

If you encounter any problems, please file an issue along with a detailed description.

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

Pre-commit hooks are set up for this project. Please make sure you have pre-commit installed and set up on this repo.

License

Distributed under the terms of the MIT license, "pytest-catch-server" is free and open source software.

Footnote

This pytest plugin was generated with Cookiecutter along with @hackebrot's cookiecutter-pytest-plugin template.

Release files for pytest-catch-server 1.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-catch-server 1.0.0
File Size Uploaded
pytest-catch-server-1.0.0.tar.gz 4.5 kB Details

Release files / pytest-catch-server-1.0.0.tar.gz

Download URL pytest-catch-server-1.0.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7a4de7a4cf1efa8b92289c9ead60bd8546b97f21f083e539572f98e6ed6f2d9b
BLAKE2b-256 checksum
How to use checksums
64a32fdef3f5ccca53b9383ade1d286c27a06a0b2251ed5b85722d6b771b88ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

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