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.sh
This creates:
- Test Dataset database on port 9999
- Test Provenance database on port 9998
- Test Redis instance on port 6380 (database 1)
# Start test databases.\tests\Start-TestDatabases.ps1
This creates:
- Test Dataset database on port 9999
- Test Provenance database on port 9998
- Test Redis instance on port 6380 (database 1)
Running Tests
Section titled “Running Tests”Install Dependencies
Section titled “Install Dependencies”# Install all dependencies including development toolspoetry install --with dev
Basic 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=xml
Test 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 logic
Cleanup
Section titled “Cleanup”Stop Test Databases
Section titled “Stop Test Databases”When finished testing:
./tests/stop-test-databases.sh
.\tests\Stop-TestDatabases.ps1