Skip to main content
Testing is a critical part of Caddy development. This guide covers testing strategies, running tests, and writing effective tests for your modules and contributions.

Running Tests

From README.md:136, Caddy uses Go’s built-in testing framework.

Run All Tests

This runs all tests in the Caddy repository.

Run Tests for Specific Module

Run Tests with Verbose Output

Run Specific Test Function

Run Tests with Coverage

Get detailed coverage report:

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

From CONTRIBUTING.md:40, optimizations should include benchmarks:
Run benchmarks:
Compare benchmarks:

Integration Tests

Caddy has integration tests in caddytest/integration/:

Best Practices

From CONTRIBUTING.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

From README.md:21, Caddy uses GitHub Actions for CI:

Race Detection

Test for race conditions:
Always run race detection before submitting PRs. Race conditions can cause subtle bugs.

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