Testing#

Muffin provides a built-in test client for testing your application.

TestClient#

Use TestClient to test HTTP requests:

Example application code:

from muffin import Application

app = Application()

@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'

WebSocket Testing#

You can also test WebSocket routes:

from muffin import Application, ResponseWebSocket

app = Application()

@app.route('/ws')
async def socket(request):
    async with ResponseWebSocket(request) as 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 Integration#

Muffin provides fixtures for integration with pytest.

Setup:

Set the module path to your Muffin application in your pytest configuration file or use the --muffin-app command-line option:

$ pytest -xs --muffin-app example

After this, the following fixtures will be available in your tests:

  • app – your Muffin application

  • client – a TestClient instance

Warning

If you get the warning: PytestCollectionWarning: cannot collect test class 'ASGITestClient', change the TestClient import to: from muffin import TestClient as Client