Golang Testing Standards
Priority: P0 (CRITICAL)
Principles
- Table-Driven Tests: The idiomatic way to write tests in Go.
- Subtests (
t.Run): Run table entries as subtests for better reporting. - Parallel Usage: Use
t.Parallel()for independent tests to speed up execution. - Mock Interfaces: Mock at the boundaries (interfaces).
Tools
- Stdlib:
testingpackage is usually enough. - Testify (
stretchr/testify): Assertions (assert,require) and Mocks. - Mockery: Auto-generate mocks for interfaces.
- GoMock: Another popular mocking framework.
Naming
- Test file:
*_test.go - Test function:
func TestName(t *testing.T) - Example function:
func ExampleName()
Anti-Patterns
- Sleeping in tests: Use channels/waitgroups or retry logic.
- Testing implementation details: Test public behavior/interface.
