$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
GobeginnerNew

Go Testing

Share

Set up Go testing with table-driven tests and mocks

Works with OpenClaude

You are a Go developer setting up comprehensive unit tests. The user wants to write table-driven tests and mock external dependencies in Go.

What to check first

  • Run go version to confirm Go 1.11+ is installed (required for go modules)
  • Check if testing package is already imported in your *_test.go files
  • Verify you have a mocking library available—github.com/golang/mock or github.com/stretchr/testify/mock are standard

Steps

  1. Create a *_test.go file in the same package as the code you're testing (e.g., calculator_test.go for calculator.go)
  2. Write a table-driven test using a slice of structs with name, input, and expected fields
  3. Loop through the test cases with for _, tt := range tests and run t.Run(tt.name, ...) for each
  4. Use t.Errorf() to fail the test with a formatted message if the actual result doesn't match expected
  5. Install the mock generation tool: go install github.com/golang/mock/mockgen@latest
  6. Generate mocks from interfaces using mockgen -source=myfile.go -destination=mocks/mock_myfile.go
  7. Import the generated mock package and use gomock.NewController(t) to create a controller for assertions
  8. Set up mock expectations with .EXPECT().MethodName().Return(value) before calling the function under test

Code

package calculator

import (
	"testing"

	"github.com/golang/mock/gomock"
)

func TestAdd(t *testing.T) {
	tests := []struct {
		name     string
		a        int
		b        int
		expected int
	}{
		{name: "positive numbers", a: 2, b: 3, expected: 5},
		{name: "negative numbers", a: -2, b: -3, expected: -5},
		{name: "mixed signs", a: 5, b: -3, expected: 2},
		{name: "zero", a: 0, b: 0, expected: 0},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := Add(tt.a, tt.b)
			if result != tt.expected {
				t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, result, tt.expected)
			}
		})
	}
}

// Example with mocks (assuming Logger interface exists)
func TestProcessWithLogger(t *testing.T) {
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()

	mockLogger := NewMockLogger(ctrl)
	mockLogger.EXPECT().
		Log(gomock

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryGo
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
gotestingtable-driven

Install command:

curl -o ~/.claude/skills/go-testing.md https://claude-skills-hub.vercel.app/skills/go/go-testing.md

Related Go Skills

Other Claude Code skills in the same category — free to download.

Want a Go skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.