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
Test structure
Section titled “Test structure”tests/├── conftest.py # Pytest configuration and fixtures├── unit/ # Unit tests│ ├── test_api.py│ ├── test_auth.py│ ├── test_editor.py│ └── ...├── integration/ # Integration tests│ ├── test_entity_integration.py│ ├── test_shacl_utils_integration.py│ └── ...Setting up test environment
Section titled “Setting up test environment”Prerequisites
Section titled “Prerequisites”- Poetry for dependency management
- Docker for test databases
- Python 3.10+ (supports 3.10, 3.11, 3.12, 3.13)
Test database setup
Section titled “Test database setup”Tests require dedicated databases running on different ports:
# Make scripts executablechmod +x ./tests/start-test-databases.shchmod +x ./tests/stop-test-databases.sh
# Start test databases./tests/start-test-databases.shThis creates:
- Test dataset database on port 9999
- Test provenance database on port 9998
# Start test databases.\tests\Start-TestDatabases.ps1This creates:
- Test dataset database on port 9999
- Test provenance database on port 9998
Running tests
Section titled “Running tests”Install dependencies
Section titled “Install dependencies”# Install all dependencies including development toolspoetry install --with devBasic test execution
Section titled “Basic test execution”# Run all testspoetry run pytest
# Run with verbose outputpoetry run pytest -v
# Run with detailed outputpoetry run pytest -vvv# Run unit tests onlypoetry run pytest tests/unit/
# Run integration tests onlypoetry run pytest tests/integration/
# Run specific test filepoetry run pytest tests/unit/test_api.py# Run specific test functionpoetry run pytest tests/unit/test_api.py::test_get_entity
# Run tests matching patternpoetry run pytest -k "test_entity"
# Run tests with specific markerpoetry run pytest -m "integration"Coverage analysis
Section titled “Coverage analysis”# Run tests with coveragepoetry run pytest --cov=heritrace
# Coverage with missing linespoetry run pytest --cov=heritrace --cov-report=term-missing# Generate HTML coverage reportpoetry 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)poetry 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(Config): TESTING = True DATASET_DB_URL = 'http://localhost:9999/sparql' PROVENANCE_DB_URL = 'http://localhost:9998/sparql' SECRET_KEY = 'test-secret-key'
# Disable external services in tests ORCID_CLIENT_ID = 'test-client-id' ORCID_CLIENT_SECRET = 'test-client-secret'Fixtures
Section titled “Fixtures”Common test fixtures are defined in conftest.py:
@pytest.fixturedef app(): """Create application instance for testing.""" app = create_app(TestConfig) return app
@pytest.fixturedef client(app): """Create test client.""" return app.test_client()
@pytest.fixturedef dataset_db(): """Dataset database connection.""" # Database setup logicCleanup
Section titled “Cleanup”Stop test databases
Section titled “Stop test databases”When finished testing:
./tests/stop-test-databases.sh.\tests\Stop-TestDatabases.ps1