This directory contains comprehensive unit tests for the FastAPI backend application.
conftest.py- Pytest configuration and shared fixtures (database, test client)test_smoke.py- Basic smoke tests to verify application startstest_settings.py- Settings and configuration teststest_conftest_exceptions.py- Tests for conftest exception handlingtest_crud.py- CRUD operation tests (database layer)test_routes_jobs.py- Job creation and retrieval endpoint teststest_routes_videos.py- Video and transcript endpoint teststest_routes_search.py- Search functionality tests (Postgres FTS and OpenSearch)test_routes_auth.py- Authentication and session management teststest_routes_billing.py- Stripe billing integration tests (mocked)test_routes_exports.py- Export functionality tests (SRT, VTT, JSON, PDF)test_schemas.py- Pydantic model validation tests
-
Install test dependencies:
pip install -r requirements-dev.txt pip install -r requirements.txt
-
Set up a PostgreSQL test database:
# Using Docker docker run --name test-postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:15 # Apply schema psql postgresql://postgres:postgres@localhost:5432/postgres -f sql/schema.sql
# Basic test run
pytest tests/
# With verbose output
pytest tests/ -v
# With coverage
pytest tests/ --cov=app --cov-report=term --cov-report=html
# Run specific test file
pytest tests/test_routes_jobs.py -v
# Run specific test
pytest tests/test_routes_jobs.py::TestJobsRoutes::test_create_job_single_success -vTests require the following environment variables:
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/postgres"
export SESSION_SECRET="test-secret-key"
export FRONTEND_ORIGIN="http://localhost:5173"test_database_url- Returns test database URL from environmenttest_engine- Creates SQLAlchemy engine for testsdb_session- Provides isolated database session with automatic rollbacksetup_test_database- Ensures database schema is initialized
client- FastAPI TestClient for making HTTP requests
- Target: 70%+ code coverage on
app/directory - Critical paths: All happy paths and error cases covered
- Mocking: External dependencies (OAuth, Stripe, OpenSearch) are properly mocked
class TestMyFeature:
"""Tests for my feature."""
def test_happy_path(self, client: TestClient):
"""Test the successful case."""
response = client.get("/my-endpoint")
assert response.status_code == 200
def test_error_case(self, client: TestClient):
"""Test error handling."""
response = client.get("/my-endpoint?invalid=param")
assert response.status_code == 400def test_with_database(self, client: TestClient, db_session):
"""Test that requires database access."""
# Create test data
db_session.execute(text("INSERT INTO ..."))
db_session.commit()
# Test endpoint
response = client.get("/endpoint")
assert response.status_code == 200from unittest.mock import patch, MagicMock
@patch("stripe.checkout.Session.create")
def test_stripe_integration(self, mock_stripe, client):
"""Test Stripe integration with mocking."""
mock_stripe.return_value = MagicMock(id="cs_test", url="https://...")
response = client.post("/billing/checkout-session", json={})
assert response.status_code == 200Tests run automatically in GitHub Actions on:
- Push to
mainbranch - Pull requests
- Manual workflow dispatch
The CI pipeline:
- Sets up Python 3.11 and 3.12
- Installs dependencies
- Starts PostgreSQL service
- Applies database schema
- Runs tests with coverage
- Generates coverage reports
- Uploads artifacts
If you see "OperationalError: could not connect to server":
- Ensure PostgreSQL is running
- Check DATABASE_URL environment variable
- Verify PostgreSQL is listening on port 5432
If you see "ProgrammingError: relation does not exist":
- Apply the database schema:
psql $DATABASE_URL -f sql/schema.sql - The test fixture handles missing schema gracefully in CI
If you see "ModuleNotFoundError":
- Ensure you're in the repository root directory
- Install all dependencies:
pip install -r requirements.txt -r requirements-dev.txt
After running tests with coverage, view the HTML report:
# Generate and open HTML coverage report
pytest tests/ --cov=app --cov-report=html
open htmlcov/index.html # macOS
xdg-open htmlcov/index.html # Linux- Test names: Use descriptive names following
test_<action>_<expected_result>pattern - Isolation: Each test should be independent and not rely on other tests
- Fixtures: Use fixtures for common setup to avoid code duplication
- Mocking: Mock external services to avoid network calls and improve speed
- Assertions: Use specific assertions with clear error messages
- Documentation: Add docstrings to test classes and methods