Skip to main content

Testing Environment Configuration

To configure etchblok-test-api for automated testing, you must initialize the application using the TestingConfig class. This ensures the application runs in a controlled environment with isolated state and predictable pagination behavior.

Initializing the Test Application

Use the create_app factory from app/__init__.py and pass the TestingConfig class as the config_class argument. This overrides the default DevelopmentConfig.

from app import create_app
from app.config import TestingConfig

def test_setup():
# Create the app instance with testing configuration
app = create_app(config_class=TestingConfig)

# Verify the configuration is active
assert app.config['TESTING'] is True
assert app.config['PAGE_SIZE'] == 5

return app

Configuration Details

The TestingConfig class in app/config.py provides specific overrides designed for test environments:

@dataclass
class TestingConfig(BaseConfig):
"""Configuration for test runs."""

TESTING: bool = True
PAGE_SIZE: int = 5
  • TESTING: Set to True to enable Flask's internal testing mode, which improves error reporting during test execution.
  • PAGE_SIZE: Reduced to 5 (from the default 25 in BaseConfig). This allows you to test pagination logic and "next page" links using a much smaller set of mock data.

Managing Test State

Because etchblok-test-api uses an in-memory repository, data persists as long as the application process is running. To ensure test isolation, you must manually clear the repository and reset services between individual test cases.

Clearing the Repository

The BookmarkRepository in app/db/repository.py includes a _clear_all method specifically for wiping all bookmarks, tags, and collections.

from app.db.repository import BookmarkRepository

repo = BookmarkRepository()

# Wipe all in-memory data between tests
repo._clear_all()

Resetting the Service

The BookmarkService is a singleton that maintains its own reference to the repository. Use the _reset() method in app/services/bookmark_service.py to clear the service's internal state.

from app.services.bookmark_service import BookmarkService

service = BookmarkService()

# Reset the service singleton state
service._reset()

Troubleshooting

Persistent State Between Tests

If you notice that data from one test is appearing in another, ensure you are calling _reset() on the BookmarkService. Since the service is a singleton, simply creating a new Flask app instance via create_app does not automatically clear the data stored in the service's underlying repository.

Pagination Logic

If your tests expect 25 items per page, they will fail under TestingConfig. Always use the PAGE_SIZE value from app.config when generating test data to ensure your assertions match the environment:

def test_pagination(app):
limit = app.config['PAGE_SIZE']
# Create exactly enough items to trigger a second page
for i in range(limit + 1):
# ... create bookmarks ...
pass