Backend Tests
This directory contains all unit and integration tests for the backend application.
Test Structure
test_database.py - Database initialization and connection management tests
Running Tests
Run all tests
cd backend
python -m pytest
Run specific test file
python -m pytest test/test_database.py
Run with coverage
python -m pytest --cov=. --cov-report=html
Run specific test class or function
python -m pytest test/test_database.py::TestDatabaseInitialization
python -m pytest test/test_database.py::TestDatabaseInitialization::test_init_database_creates_tables
Test Organization
Tests are organized into classes by functionality:
- TestDatabaseInitialization: Tests for database table creation and schema validation
- TestConnectionManagement: Tests for database connection handling and lifecycle
- TestDatabaseIntegrity: Tests for database constraints and referential integrity
Writing Tests
When adding new tests:
- Create test files with
test_ prefix (e.g., test_routers_project.py)
- Organize tests into classes with
Test prefix
- Name test functions with
test_ prefix
- Use descriptive names that explain what is being tested
- Add docstrings explaining the test purpose and requirements validated
- Use fixtures for common setup/teardown
- Follow the Arrange-Act-Assert pattern
Test Coverage
Current test coverage focuses on:
- ✅ Database initialization (Requirements 9.1)
- ✅ Connection management (Requirements 9.2)
- ✅ Table schema validation
- ✅ Foreign key constraints
- ✅ Cascade deletes
- ✅ Transaction handling (commit/rollback)
Dependencies
- pytest==7.4.3
- pytest-cov==4.1.0
See requirements.txt for full dependency list.