Testing#

TestClient#

The test client allows you to test requests against your ASGI application.

The application code:

from muffin import Application

app = Application()

# Test HTTP requests
@app.route('/')
async def home(request):
    return "OK"

Tests:

from muffin import TestClient

client = TestClient(app)
response = await client.get('/')
assert response.status_code == 200
assert await response.text() == 'OK'

Websockets:#

from muffin import Application, ResponseWebSocket

app = Application()

@app.route('/ws')
async def socket(request):
    ws = ResponseWebSocket(request)
    async with ws:
        msg = await ws.receive()
        if msg == 'ping':
            await ws.send('pong')

Test Websockets:

from muffin import TestClient

client = TestClient(app)

async with client.websocket('/ws') as ws:
    await ws.send('ping')
    msg = await ws.receive()
    assert msg == 'pong'

Check the TestClient API Reference: TestClient

Pytest Support#

Set module path to your Muffin Application in pytest configuration file or use command line option --muffin-app.

Example:

$ py.test -xs --muffin-app example

After that the fixtures app, client will be available for your tests.

Warning

if you get a warning from pytest: "PytestCollectionWarning: cannot collect test class 'ASGITestClient' change TestClient import from muffin import TestClient as Client