Go testing practices - unit tests, benchmarks, mocks, coverage
View on GitHubpluginagentmarketplace/custom-plugin-go
go-development-assistant
January 21, 2026
Select agents to install to:
npx add-skill https://github.com/pluginagentmarketplace/custom-plugin-go/blob/main/skills/go-testing/SKILL.md -a claude-code --skill go-testingInstallation paths:
.claude/skills/go-testing/# Go Testing Skill
Comprehensive testing strategies for Go applications.
## Overview
Master Go testing including table-driven tests, benchmarks, mocking, fuzzing, and achieving high code coverage.
## Parameters
| Parameter | Type | Required | Default | Description |
|-----------|------|----------|---------|-------------|
| test_type | string | yes | - | Type: "unit", "integration", "benchmark", "fuzz" |
| coverage_min | int | no | 80 | Minimum coverage percentage |
| mock_lib | string | no | "testify" | Mock library: "testify", "gomock" |
## Core Topics
### Table-Driven Tests
```go
func TestParseURL(t *testing.T) {
tests := []struct {
name string
input string
want *URL
wantErr bool
}{
{
name: "valid http",
input: "http://example.com/path",
want: &URL{Scheme: "http", Host: "example.com", Path: "/path"},
},
{
name: "empty string",
input: "",
wantErr: true,
},
{
name: "invalid scheme",
input: "ftp://example.com",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseURL(tt.input)
if tt.wantErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
```
### Mocking
```go
type MockStore struct {
mock.Mock
}
func (m *MockStore) Get(ctx context.Context, key string) (string, error) {
args := m.Called(ctx, key)
return args.String(0), args.Error(1)
}
func TestService_GetValue(t *testing.T) {
store := new(MockStore)
store.On("Get", mock.Anything, "key1").Return("value1", nil)
svc := NewService(store)
val, err := svc.GetValue(context.Background(), "key1")
require.NoError(t, err)
assert.Equal(t, "value1", val)
stor