Warning: Bocadillo is now UNMAINTAINED. Users are recommended to migrate to a supported alternative, such as Starlette or FastAPI. Please see #344 for more information.

Testing with pytest

This page lists what we believe are best practices when testing Bocadillo applications using the pytest test framework.

Fixtures

When using pytest, we recommend you setup some fixtures to provision an application instance, a test client and a live server. By doing so, you'll be able to write tests in a more concise fashion.

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

Example test:

# 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()
    ...