Flask 3 cheatsheet

Flask 3: Testing and deployment

4 common Flask 3 tasks.

Test with the test client

Call routes without running a server.

def test_index(client):
    response = client.get("/")
    assert response.status_code == 200

Create a pytest fixture

Make a test client available to tests.

import pytest

@pytest.fixture
def client():
    app = create_app()
    app.config["TESTING"] = True
    return app.test_client()

Run with Gunicorn

Serve the app with a production WSGI server.

gunicorn -w 4 -b 0.0.0.0:8000 "app:create_app()"

Trust a reverse proxy

Read the real client IP and scheme when behind Nginx.

from werkzeug.middleware.proxy_fix import ProxyFix

app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)

Was this page helpful?