Skip to content

Running Tests

HERITRACE uses pytest for testing. This guide covers running tests locally and understanding the testing infrastructure.

  • Unit Tests: Test individual functions and classes
  • Integration Tests: Test component interactions and database operations
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
│ └── ...
  • Poetry for dependency management
  • Docker for test databases
  • Python 3.10+ (supports 3.10, 3.11, 3.12, 3.13)

Tests require dedicated databases running on different ports:

Terminal window
# Make scripts executable
chmod +x ./tests/start-test-databases.sh
chmod +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)
Terminal window
# Install all dependencies including development tools
poetry install --with dev
Terminal window
# Run all tests
poetry run pytest
# Run with verbose output
poetry run pytest -v
# Run with detailed output
poetry run pytest -vvv
Terminal window
# Run tests with coverage
poetry run pytest --cov=heritrace
# Coverage with missing lines
poetry run pytest --cov=heritrace --cov-report=term-missing

Tests use a separate configuration file:

tests/test_config.py
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'

Common test fixtures are defined in conftest.py:

@pytest.fixture
def app():
"""Create application instance for testing."""
app = create_app(TestConfig)
return app
@pytest.fixture
def client(app):
"""Create test client."""
return app.test_client()
@pytest.fixture
def dataset_db():
"""Dataset database connection."""
# Database setup logic

When finished testing:

Terminal window
./tests/stop-test-databases.sh