Skip to main content

Overview

Comprehensive testing ensures reliability, maintainability, and confidence in deployments. This guide covers unit testing, integration testing, property-based testing, load testing, and security testing.
We aim for >80% code coverage with a multi-layered testing approach including unit, integration, property-based, and end-to-end tests.

Testing Pyramid

Unit Tests

  • Fast (< 1s)
  • Isolated
  • Mocked dependencies
  • 70-80% of tests

Integration Tests

  • Real services
  • Database, Redis, APIs
  • 15-20% of tests
  • Slower (seconds)

E2E Tests

  • Full system
  • User workflows
  • 5-10% of tests
  • Slowest (minutes)

Test Organization

Directory Structure

Test Markers

Usage:

Unit Testing

Writing Unit Tests

Mocking Dependencies

Testing Async Code

Integration Testing

Setup Test Environment

Integration Tests

API Integration Tests

Property-Based Testing

Using Hypothesis

Load Testing

Using Locust

Run load test:

Using k6

Run k6 test:

Security Testing

SQL Injection Tests

Authentication Tests

Continuous Integration

GitHub Actions Workflow

Test Coverage

Measuring Coverage

Coverage Configuration

Best Practices

Test Dependency Management

Critical: All packages imported in tests must be available in project dependencies. Missing test dependencies cause CI failures that pass locally.

The Problem

On 2025-11-12, 10 CI jobs failed with ModuleNotFoundError: docker.errors despite tests passing locally:
  • Root cause: docker and kubernetes packages were in code-execution extras, not dev extras
  • Impact: CI workflows install dev extras but tests import docker modules
  • Result: Tests pass locally (all extras installed) but fail in CI (only dev installed)

Prevention Strategy

Three layers of protection prevent this issue:
1

Regression Test

File: tests/regression/test_dev_dependencies.pyValidates all test imports against project dependencies:
Runs: On every commit in CI, catches missing dependencies immediately.
2

Pre-commit Hook

Hook ID: validate-test-dependenciesRuns dependency validation before each commit:
Fast: Only runs affected test, typically < 5 seconds.
3

Workflow Validation

Script: scripts/validation/validate_workflow_test_deps.pyValidates GitHub Actions workflows:
Hook ID: validate-workflow-test-deps (runs on workflow file changes)

Dependency Rules

If a test file imports a package, that package must be in project dependencies.
Why dev extras?
  • All test workflows install dev extras by default
  • Keeps test dependencies separate from production code
  • Matches pytest, pytest-cov, pytest-mock pattern
Some packages have different import names:The regression test handles these automatically via get_import_name_mapping().
Python 3.11+ includes tomllib in stdlib. Since the project minimum is Python 3.11, use tomllib directly:
The tomli backport is no longer needed or allowed.
Third-party test tools (e.g., bats-core) have their own dependencies:
The regression test skips test_helper/ directories automatically.

Quick Reference

Troubleshooting

Symptom: ModuleNotFoundError in CI, test passes on your machine.Cause: Package installed locally but not in dev extras.Solution:
  1. Run dependency validation: pytest tests/regression/test_dev_dependencies.py -v
  2. Add missing package to [project.optional-dependencies.dev]
  3. Run uv sync --extra dev to update lockfile
  4. Re-run tests locally to verify
Example:
Symptom: validate-test-dependencies hook fails on commit.Cause: New test import without corresponding dependency.Solution:
  1. Check hook output for missing packages
  2. Add packages to appropriate extras in pyproject.toml:
    • Test dependencies → dev
    • Code execution → code-execution
    • CLI tools → cli
  3. Run uv sync --extra dev
  4. Re-run pre-commit: pre-commit run validate-test-dependencies --all-files
Symptom: validate-workflow-test-deps hook fails.Cause: Workflow runs pytest but doesn’t install dev extras.Solution: Update workflow to include dev in extras:
Historical Context: This protection system was implemented after commit 7b51437 (2025-11-12) to prevent the recurrence of 10 simultaneous CI failures. The regression test, pre-commit hooks, and workflow validation ensure this class of error can never happen again.

Next Steps

Development Setup

Development environment guide

Contributing

Contributing guidelines

CI/CD

Continuous integration

Monitoring

Production monitoring

Testing Complete: Comprehensive test coverage for reliable deployments!