Running tests
HERITRACE uses pytest for testing. This guide covers running tests locally and understanding the testing infrastructure.
Test overview
Section titled “Test overview”Test types
Section titled “Test types”- Unit tests: Test individual functions and classes
- Integration tests: Test component interactions and database operations
Setting up test environment
Section titled “Setting up test environment”Prerequisites
Section titled “Prerequisites”- uv for dependency management
- Docker for test databases
- Python 3.10+ (supports 3.10, 3.11, 3.12, 3.13)
Test database lifecycle
Section titled “Test database lifecycle”Test databases start and stop automatically. A session-scoped pytest fixture (docker_services in tests/conftest.py) runs docker compose -f tests/docker-compose.yml up -d --wait before the first test and docker compose down after the last test. No manual setup or teardown is needed.
The test services and their host ports:
- Dataset database (Virtuoso): port 41800
- Provenance database (Virtuoso): port 41802
- Redis: port 41804
Running tests
Section titled “Running tests”Install dependencies
Section titled “Install dependencies”# Install all dependencies including development toolsuv sync --locked --devBasic test execution
Section titled “Basic test execution”# Run all testsuv run pytest
# Run with verbose outputuv run pytest -v
# Run with detailed outputuv run pytest -vvv# Run unit tests onlyuv run pytest tests/unit/
# Run integration tests onlyuv run pytest tests/integration/
# Run specific test fileuv run pytest tests/unit/test_api.py# Run specific test functionuv run pytest tests/unit/test_api.py::test_get_entity
# Run tests matching patternuv run pytest -k "test_entity"
# Run tests with specific markeruv run pytest -m "integration"Coverage analysis
Section titled “Coverage analysis”# Run tests with coverageuv run pytest --cov=heritrace
# Coverage with missing linesuv run pytest --cov=heritrace --cov-report=term-missing# Generate HTML coverage reportuv run pytest --cov=heritrace --cov-report=html
# Open the reportopen htmlcov/index.html # macOSxdg-open htmlcov/index.html # Linuxstart htmlcov/index.html # Windows# Generate XML coverage (for CI/CD)uv run pytest --cov=heritrace --cov-report=xmlTest configuration
Section titled “Test configuration”Test settings
Section titled “Test settings”Tests use a separate configuration file:
class TestConfig(object): TESTING = True DATASET_DB_URL = "http://localhost:41800/sparql" PROVENANCE_DB_URL = "http://localhost:41802/sparql" REDIS_URL = "redis://localhost:41804/0" SECRET_KEY = "test-secret-key-for-testing-only" CACHE_VALIDITY_DAYS = 1
ORCID_CLIENT_ID = "test-client-id" ORCID_CLIENT_SECRET = "test-client-secret" # ...Fixtures
Section titled “Fixtures”Common test fixtures are defined in tests/conftest.py:
@pytest.fixture(scope="session", autouse=True)def docker_services(): subprocess.run( ["docker", "compose", "-f", COMPOSE_FILE, "up", "-d", "--wait"], check=True, ) yield subprocess.run( ["docker", "compose", "-f", COMPOSE_FILE, "down"], check=True, )
@pytest.fixturedef app(): app = create_app(TestConfig) with app.app_context(): yield app
@pytest.fixturedef client(app): return app.test_client()Test databases are stopped automatically when the pytest session ends.