Skip to main content

Pytest plugin with server for catching HTTP requests.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-catch-server-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file pytest-catch-server-1.0.0.tar.gz.

File metadata

  • Download URL: pytest-catch-server-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pytest-catch-server-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7a4de7a4cf1efa8b92289c9ead60bd8546b97f21f083e539572f98e6ed6f2d9b
MD5 82beb40b3e94ff360263bcc12a2c79f4
BLAKE2b-256 64a32fdef3f5ccca53b9383ade1d286c27a06a0b2251ed5b85722d6b771b88ff

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page