Overview
Testing is mandatory for all code changes in GOV.UK Notify Admin. We maintain comprehensive test coverage across Python backend code, JavaScript frontend code, and integration points.Test Types
Unit Tests (Python)
Test individual functions and classes in isolation.Integration Tests (Python)
Test interactions between components and external services.Frontend Tests (JavaScript)
Test JavaScript modules and UI interactions using Jest.End-to-End Tests
Manual testing workflows for critical user journeys.Python Testing
Test Framework: pytest
We use pytest for all Python tests. Configuration is inpytest.ini:
Running Python Tests
Writing Python Tests
Test Structure
Organize tests to mirror the application structure:Test Naming
- Test files:
test_<module_name>.py - Test functions:
test_<description_of_behavior> - Be descriptive:
test_user_cannot_access_another_service
Using Fixtures
Parametrized Tests
Test multiple scenarios with one test function:Testing Exceptions
Using xfail for Expected Failures
Mocking External Services
Testing Flask Views
Test Coverage Requirements
- New features: Minimum 90% coverage
- Bug fixes: Must include regression test
- Refactoring: Coverage must not decrease
JavaScript Testing
Test Framework: Jest
JavaScript tests use Jest with jsdom for DOM testing. Configuration inpackage.json:
Running JavaScript Tests
Writing JavaScript Tests
Test Structure
Testing DOM Manipulation
Mocking in Jest
Testing Event Handlers
Jest Configuration for GOV.UK Notify
Disabled tests are errors:.only or .skip:
Integration Testing
Testing with External Services
Mock external API calls:Testing Database Interactions
Use fixtures to set up test data:Test Data Management
Fixtures in conftest.py
Shared fixtures for all tests:
Factory Functions
Create test data programmatically:Continuous Integration
All tests run automatically on pull requests:- Python linting: Ruff checks code style
- JavaScript linting: ESLint and Stylelint
- Python tests: pytest with coverage
- JavaScript tests: Jest
Local CI Simulation
Run the full test suite locally:Best Practices
Test Independence
- Tests must not depend on execution order
- Each test should set up its own data
- Clean up after each test
Descriptive Test Names
One Assertion Per Test (When Possible)
Test Edge Cases
Mock External Dependencies
Testing Checklist
Before submitting a PR:- All tests pass locally (
make test) - New code has test coverage
- Edge cases are tested
- Error conditions are tested
- Integration points are tested
- No focused or skipped tests (
.only,.skip) - Test names are descriptive
- Mocks are used appropriately
- Tests are independent and can run in any order