Flask-Testing
Flask-Testing is a stable Python library designed to provide unit testing utilities for Flask applications. It integrates seamlessly with Python's built-in `unittest` module, offering `TestCase` and `LiveServerTestCase` classes that simplify testing Flask components, including routes, templates, and live server interactions. Currently at version 0.8.1, the library maintains a moderate release cadence, focusing on stability and compatibility with Flask's ecosystem.
Warnings
- breaking Flask-Testing v0.8.0 dropped official support for Python 2.6, 3.3, and 3.4. Users on these older Python versions should use an earlier Flask-Testing version or upgrade their Python environment.
- breaking Versions of Flask-Testing prior to v0.8.0 may have compatibility issues with Werkzeug 1.0 due to changes in import paths within Werkzeug.
- deprecated The import path `from flask.ext.testing import ...` is deprecated. The correct modern import path is `from flask_testing import ...`.
- gotcha Subclasses of `TestCase` and `LiveServerTestCase` *must* implement a `create_app` method that returns a Flask application instance. Failure to do so will result in a `NotImplementedError`.
- gotcha For features like `assertTemplateUsed` and `get_context_variable`, the `blinker` library must be installed. Without it, these methods might not function correctly or prevent tests from running.
- gotcha When using `LiveServerTestCase` for parallel testing, explicitly set `app.config['LIVESERVER_PORT'] = 0` within your `create_app` method to allow the operating system to dynamically assign an available port. Otherwise, tests might conflict over the default port (5000).
Install
-
pip install flask-testing
Imports
- TestCase
from flask_testing import TestCase
- LiveServerTestCase
from flask_testing import LiveServerTestCase
Quickstart
import os
from flask import Flask, jsonify
from flask_testing import TestCase, LiveServerTestCase
# A simple Flask application to test
def create_test_app():
app = Flask(__name__)
app.config['TESTING'] = True
app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'default_secret_key')
@app.route('/')
def index():
return 'Hello Flask-Testing!'
@app.route('/data')
def get_data():
return jsonify({'message': 'Data retrieved!'})
return app
class MyUnitTests(TestCase):
def create_app(self):
return create_test_app()
def test_index_page(self):
response = self.client.get('/')
self.assert200(response)
self.assertIn(b'Hello Flask-Testing!', response.data)
def test_json_data(self):
response = self.client.get('/data')
self.assert200(response)
self.assertContentType('application/json', response)
self.assertEqual(response.json, {'message': 'Data retrieved!'})
class MyLiveServerTests(LiveServerTestCase):
def create_app(self):
app = create_test_app()
app.config['LIVESERVER_PORT'] = 0 # Let OS pick an available port
return app
def test_server_running_and_accessible(self):
import urllib.request
response = urllib.request.urlopen(self.get_server_url())
self.assertEqual(response.code, 200)
self.assertIn(b'Hello Flask-Testing!', response.read())