Go Testing
This skill provides comprehensive Go testing patterns extending common testing principles with Go-specific idioms.
Testing Framework
Use the standard go test with table-driven tests as the primary pattern.
Table-Driven Tests
The idiomatic Go testing pattern:
Benefits:
- Easy to add new test cases
- Clear test case documentation
- Parallel test execution with
t.Parallel() - Isolated subtests with
t.Run()
Test Helpers
Use t.Helper() to mark helper functions:
Benefits:
- Correct line numbers in test failures
- Reusable test utilities
- Cleaner test code
Test Fixtures
Use t.Cleanup() for resource cleanup:
Race Detection
Always run tests with the -race flag to detect data races:
In CI/CD:
Why:
- Detects concurrent access bugs
- Prevents production race conditions
- Minimal performance overhead in tests
Coverage Analysis
Basic Coverage
Detailed Coverage Report
Coverage Thresholds
Benchmarking
Run benchmarks:
Compare benchmarks:
Mocking
Interface-Based Mocking
Integration Tests
Build Tags
Run integration tests:
Test Containers
Test Organization
File Structure
Package Naming
Common Patterns
Testing HTTP Handlers
Testing with Context
Best Practices
- Use
t.Parallel()for independent tests - Use
testing.Short()to skip slow tests - Use
t.TempDir()for temporary directories - Use
t.Setenv()for environment variables - Avoid
init()in test files - Keep tests focused - one behavior per test
- Use meaningful test names - describe what's being tested
When to Use This Skill
- Writing new Go tests
- Improving test coverage
- Setting up test infrastructure
- Debugging flaky tests
- Optimizing test performance
- Implementing integration tests


