This page lists what we believe are best practices when testing Bocadillo applications using the pytest test framework.
You can use this sample
conftest.py as a starting point:
# conftest.py import pytest from bocadillo import configure, create_client, LiveServer from app import app configure(app) @pytest.fixture def app(): return app @pytest.fixture def client(app): return create_client(app) @pytest.fixture def server(app): with LiveServer(app) as server: yield server
# tests.py def test_hello(client): response = client.get("/hello") assert response.status_code == 200 assert response.text = "Hello, pytest!"
Testing asynchronous code
You can use the pytest-asyncio extension if you need to test asynchronous code.
Among other things, it provides an
asyncio mark that allows to run
async test functions, as well as the ability to write async fixtures.
# tests.py import pytest from myproject import some_async_function @pytest.mark.asyncio async def test_render(): result = await some_async_function() ...