{"id":4542,"library":"flask-testing","title":"Flask-Testing","description":"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.","status":"active","version":"0.8.1","language":"en","source_language":"en","source_url":"https://github.com/jarus/flask-testing","tags":["flask","testing","unit-testing","integration-testing","web-development"],"install":[{"cmd":"pip install flask-testing","lang":"bash","label":"Install Flask-Testing"}],"dependencies":[{"reason":"Core dependency as Flask-Testing is an extension for Flask applications.","package":"Flask","optional":false},{"reason":"Required for `assertTemplateUsed` and `get_context_variable` functionality, which relies on Flask's signals. (Optional for basic usage)","package":"Blinker","optional":true}],"imports":[{"note":"The `flask.ext.testing` import path was deprecated in Flask-Testing v0.4.0 and should no longer be used.","wrong":"from flask.ext.testing import TestCase","symbol":"TestCase","correct":"from flask_testing import TestCase"},{"note":"The `flask.ext.testing` import path was deprecated in Flask-Testing v0.4.0 and should no longer be used.","wrong":"from flask.ext.testing import LiveServerTestCase","symbol":"LiveServerTestCase","correct":"from flask_testing import LiveServerTestCase"}],"quickstart":{"code":"import os\nfrom flask import Flask, jsonify\nfrom flask_testing import TestCase, LiveServerTestCase\n\n# A simple Flask application to test\ndef create_test_app():\n    app = Flask(__name__)\n    app.config['TESTING'] = True\n    app.config['SECRET_KEY'] = os.environ.get('FLASK_SECRET_KEY', 'default_secret_key')\n\n    @app.route('/')\n    def index():\n        return 'Hello Flask-Testing!'\n\n    @app.route('/data')\n    def get_data():\n        return jsonify({'message': 'Data retrieved!'})\n\n    return app\n\nclass MyUnitTests(TestCase):\n    def create_app(self):\n        return create_test_app()\n\n    def test_index_page(self):\n        response = self.client.get('/')\n        self.assert200(response)\n        self.assertIn(b'Hello Flask-Testing!', response.data)\n\n    def test_json_data(self):\n        response = self.client.get('/data')\n        self.assert200(response)\n        self.assertContentType('application/json', response)\n        self.assertEqual(response.json, {'message': 'Data retrieved!'})\n\n\nclass MyLiveServerTests(LiveServerTestCase):\n    def create_app(self):\n        app = create_test_app()\n        app.config['LIVESERVER_PORT'] = 0 # Let OS pick an available port\n        return app\n\n    def test_server_running_and_accessible(self):\n        import urllib.request\n        response = urllib.request.urlopen(self.get_server_url())\n        self.assertEqual(response.code, 200)\n        self.assertIn(b'Hello Flask-Testing!', response.read())\n","lang":"python","description":"This quickstart demonstrates basic usage of `TestCase` for unit tests with Flask's test client, and `LiveServerTestCase` for tests requiring a running server, such as integration with browser automation tools. Ensure your Flask app is properly configured for testing and the `create_app` method is implemented in your test classes. Using `LIVESERVER_PORT = 0` allows the operating system to dynamically assign an available port, which is useful for parallel test execution."},"warnings":[{"fix":"Upgrade to Python 3.5+ or pin `flask-testing<0.8.0`.","message":"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.","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"Upgrade Flask-Testing to v0.8.0 or newer to ensure compatibility with Werkzeug 1.0+.","message":"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.","severity":"breaking","affected_versions":"<0.8.0"},{"fix":"Update your import statements to use `from flask_testing import TestCase` or `from flask_testing import LiveServerTestCase`.","message":"The import path `from flask.ext.testing import ...` is deprecated. The correct modern import path is `from flask_testing import ...`.","severity":"deprecated","affected_versions":"All versions since v0.4.0"},{"fix":"Define `def create_app(self):` in your test class and return your configured Flask app from it.","message":"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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install `blinker` if you plan to use features that rely on Flask's signals: `pip install blinker`.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Add `app.config['LIVESERVER_PORT'] = 0` to your `create_app` method in `LiveServerTestCase` subclasses.","message":"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).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}