Last updated:
0 purchases
pytestcatchserver 1.0.0
Pytest Catch Server
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.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.