| name | testing |
| description | Testing patterns including table-driven tests, mock setup, and coverage requirements. Use when writing tests or setting up test infrastructure. |
| userInvokable | true |
Testing
References: Examples
Rules
| Rule | Value |
|---|
| Unit tests | TestFunctionName |
| Integration tests | TestIntegration* |
| Coverage | 85% minimum |
| Tools | testify/assert, gomock |
Mock Generation
Example
Add at top of file with interfaces:
Run with:
make generate
Table-Driven Tests
Example
Use for testing multiple scenarios:
func TestFunction(t *testing.T) {
tests := []struct {
name string
input string
expected string
wantErr bool
}{
{name: "valid input", input: "foo", expected: "bar"},
{name: "empty input", input: "", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
})
}
}
Mock Setup
Example
func TestService(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockRepo := mocks.NewMockRepository(ctrl)
mockRepo.EXPECT().
FindByID(gomock.Any(), "id").
Return(&Entity{}, nil)
svc := NewService(mockRepo)
}
Assertions
Example
Use testify/assert:
assert.NoError(t, err)
assert.Equal(t, expected, actual)
assert.Nil(t, result)
assert.NotNil(t, result)
assert.True(t, condition)
assert.Contains(t, slice, element)