Running Tests
FromREADME.md:136, Caddy uses Go’s built-in testing framework.
Run All Tests
Run Tests for Specific Module
Run Tests with Verbose Output
Run Specific Test Function
Run Tests with Coverage
Test File Structure
Test files follow Go conventions:- Named
*_test.go(e.g.,module_test.go) - In the same package as the code being tested
- Test functions start with
Test(e.g.,TestProvision)
Example Test File Structure
Testing Module Lifecycle
Testing CaddyModule()
Testing Provision
Testing Validate
Testing HTTP Handlers
Basic Handler Test
Based on patterns from the codebase:Testing with Context and Replacer
Testing Configuration Unmarshaling
Testing JSON Unmarshaling
Testing Caddyfile Unmarshaling
Table-Driven Tests
From Go best practices, use table-driven tests for multiple scenarios:Benchmarking
FromCONTRIBUTING.md:40, optimizations should include benchmarks:
Integration Tests
Caddy has integration tests incaddytest/integration/:
Best Practices
FromCONTRIBUTING.md:38 and Go testing conventions:
1
Write tests for new code
Every new feature or bug fix should include tests.
2
Test edge cases
Test boundary conditions, empty inputs, nil values, etc.
3
Use descriptive test names
4
Keep tests independent
Tests should not depend on each other or global state.
5
Use t.Helper() for test utilities
6
Clean up resources
7
Test actual behavior
From
CONTRIBUTING.md:173, make sure tests fail without the change and pass with it.CI/CD Testing
FromREADME.md:21, Caddy uses GitHub Actions for CI:
Race Detection
Test for race conditions:Testing Tips
Mock Next Handler
Test Error Handling
Test with Different Configurations
Next Steps
Contributing
Submit your tested code
Module Development
Build modules with testing in mind
Building from Source
Set up your test environment
Plugin Tutorial
Build a fully-tested plugin