pytest-flask

1.3.0 · active · verified Sat Apr 11

pytest-flask is an extension for the pytest test runner, providing a set of useful fixtures and tools to simplify the testing and development of Flask applications and extensions. It is currently at version 1.3.0 and actively maintained, with releases typically tied to Flask and pytest major version updates and bug fixes.

Warnings

Install

Imports

Quickstart

To get started, define your Flask application factory in a module (e.g., `myapp.py`), then create a `pytest` fixture named `app` in your `conftest.py` file. This fixture will create and configure your Flask application for testing. The `client` fixture, provided by `pytest-flask`, will then be available in your tests to make requests against your application. Run tests with `pytest`.

# myapp.py
from flask import Flask

def create_app():
    app = Flask(__name__)

    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    return app

# conftest.py
import pytest
from myapp import create_app

@pytest.fixture
def app():
    app = create_app()
    app.config.update({"TESTING": True}) # Recommended for testing
    yield app

@pytest.fixture
def client(app):
    return app.test_client()

# test_app.py
def test_hello_world(client):
    response = client.get('/hello')
    assert response.status_code == 200
    assert b'Hello, World!' in response.data

view raw JSON →