Configuring the Test Suite
To ensure isolated and predictable test runs, you can utilize the TestingConfig class when initializing your application. This configuration specifically reduces the default page size to facilitate easier testing of pagination logic and sets the standard Flask TESTING flag.
Initializing the App with Testing Configuration
To use the testing configuration, pass the TestingConfig class to the create_app factory function. By default, create_app uses DevelopmentConfig, so you must explicitly provide the testing class for your test suite.
from app import create_app
from app.config import TestingConfig
def test_my_feature():
# Initialize the app with TestingConfig
app = create_app(config_class=TestingConfig)
client = app.test_client()
# The app now uses PAGE_SIZE = 5
response = client.get('/api/bookmarks/')
assert response.status_code == 200
Key Configuration Properties
The TestingConfig class in app/config.py overrides several defaults from BaseConfig to optimize for a test environment:
TESTING: Set toTrue. This enables better error reporting and allows Flask extensions to change their behavior for tests.PAGE_SIZE: Set to5. This is significantly smaller than theDevelopmentConfig(10) orProductionConfig(25), allowing you to test pagination logic (like "next" links) with just a few records.
Using in Pytest Fixtures
In a typical project setup, you should define a fixture in a conftest.py file to ensure all tests use the correct configuration consistently.
import pytest
from app import create_app
from app.config import TestingConfig
@pytest.fixture
def app():
"""Create and configure a new app instance for each test."""
app = create_app(TestingConfig)
yield app
@pytest.fixture
def client(app):
"""A test client for the app."""
return app.test_client()
Troubleshooting
Unexpected Pagination Results
If your tests are failing because they expect 10 or 25 items per page, verify that you are explicitly passing TestingConfig to create_app. The factory in app/__init__.py defaults to DevelopmentConfig if no argument is provided:
# app/__init__.py
def create_app(config_class=DevelopmentConfig) -> Flask:
# ...
Missing Secret Key
While ProductionConfig requires the SECRET_KEY environment variable to be set, TestingConfig inherits a default value of "change-me" from BaseConfig. You do not need to set environment variables to run tests using TestingConfig.