Registry / testing / pytest-flask

pytest-flask

JSON →
library1.3.0pypypi✓ verified 25d ago

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.

pip install pytest-flask
INSTALL
IMPORT
SIG · PYTEST-FLASK
P
pytest-flask
testingpythonv1.3.0
Install
3.3s avg
Import
405ms
Disk
35MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.418s · 35.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.3s · import 0.392s · 36MB
35MB installed
● package 35MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

app (fixture)
import pytest # @pytest.fixture def app(): ...
pytest-flask fixtures like 'app', 'client', 'live_server' are auto-discovered by pytest and typically not imported directly from pytest_flask. You define your 'app' fixture, and then 'client' and 'live_server' use it.
client (fixture)
def test_something(client): ...
Accessed as an argument in your test functions after defining the 'app' fixture.
live_server (fixture)
def test_live_server_access(live_server): ...
Accessed as an argument in your test functions after defining the 'app' fixture.

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
Debug
Known issues
breakingThe `request_ctx` fixture was removed in version 1.3.0 due to compatibility changes with Flask 3.0.0. Older versions of pytest-flask using this fixture will be incompatible with Flask 3.0.0+.
fix
Update to pytest-flask 1.3.0+ and refactor tests to not use `request_ctx`. Consider `app.test_request_context()` for manual context manipulation if necessary.
affects: >=1.3.0 (removal), <1.3.0 (incompatibility with Flask 3.0.0)
breakingThe `live_server.url` method was removed in version 1.2.0. Direct access to the live server URL should use string formatting or `flask.url_for` with `_external=True`.
fix
Remove `.url` calls. Construct URLs manually or use `flask.url_for(..., _external=True)` if testing a live server.
affects: >=1.2.0
breakingStarting from version 1.0.0, the `live_server` fixture became session-scoped by default. This changes its lifecycle from function-scoped, potentially impacting tests expecting a fresh server for each test function.
fix
If a function-scoped live server is required, configure `live-server_scope = function` in your `pytest.ini` file.
affects: >=1.0.0
breakingSupport for Python 2.7 and 3.4 was dropped in version 1.0.0. Ensure your testing environment uses Python 3.5 or newer.
fix
Upgrade your Python environment to 3.5 or a later supported version (currently Python >=3.7 is required).
affects: >=1.0.0
gotchaOlder versions of `pytest-flask` might encounter incompatibilities with newer versions of Werkzeug (e.g., Werkzeug >=3.1). Check release notes and open issues for specific version requirements.
fix
Always keep `pytest-flask` updated to the latest version to ensure compatibility with recent Flask and Werkzeug releases. If encountering issues, check `pytest-flask`'s GitHub issues for known incompatibilities.
affects: <1.3.0 (potentially with Werkzeug >=3.1)
Errors
Common errors & fixes
ImportError: cannot import name 'create_app'
The 'create_app' function is not properly imported due to incorrect module path or missing '__init__.py' in the package.
fix
Ensure the module containing 'create_app' is correctly structured and accessible; add '__init__.py' files as needed.
ModuleNotFoundError: No module named 'flask'
The Flask module is not installed in the current Python environment.
fix
Install Flask using 'pip install flask' in the appropriate environment.
ImportError: cannot import name '_request_ctx_stack' from 'flask'
Incompatibility between Flask and pytest versions or missing dependencies.
fix
Ensure compatibility between Flask and pytest versions; consider reinstalling or updating packages.
ImportError: cannot import name 'url_quote' from 'werkzeug.urls'
Version conflict between Flask and Werkzeug due to recent updates.
fix
Pin Werkzeug to a compatible version in 'requirements.txt', e.g., 'Werkzeug==2.2.2'.
ImportError: No module named 'config'
The 'config' module is missing or not included in the Python path.
fix
Ensure the 'config' module exists and is accessible; check 'sys.path' settings.
Upgrade
Version history
1.3.0latest on PyPI · released Oct 23, 2023
Audit
Dependencies
pytestrequiredCore testing framework that pytest-flask extends.
FlaskrequiredThe web framework being tested.
Agent activity
9 hits · last 30 days
node
7
Resources
pytest-flask — pip install pytest-flask · libregistry