golang-testing

Implements Go testing patterns including table-driven tests, benchmarks, fuzzing, and coverage analysis.

5|Updated Jul 8, 2019
One-click install
npx skills add https://github.com/rinchsan/dotfiles --skill golang-testing-rinchsan
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-testing
Source: https://github.com/rinchsan/dotfiles/tree/main/.claude/skills/golang-testing
Command: npx skills add https://github.com/rinchsan/dotfiles --skill golang-testing-rinchsan

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing comprehensive, idiomatic Go tests requires knowing many patterns—table-driven tests, subtests, mocks, benchmarks, fuzzing, and coverage tooling. This Skill provides ready-to-apply templates and TDD workflows so you write correct tests faster without searching documentation. ## Core Features & Use Cases - Table-Driven Tests & Subtests: Standard Go patterns for comprehensive coverage, including error cases, parallel subtests, and test helpers with t.Helper() and t.Cleanup(). - Benchmarks & Fuzzing: Templates for performance benchmarks with memory allocation tracking and Go 1.18+ fuzz tests with seed corpora and property-based assertions. - Coverage & CI Integration: Commands for coverage profiles, race detection, golden file testing, HTTP handler testing, and GitHub Actions pipeline setup. - Use Case: When implementing a new Go function, follow the RED-GREEN-REFACTOR cycle: write a failing table-driven test first, implement minimal code to pass, then refactor while keeping tests green. ## Quick Start Write a table-driven test with subtests for my Go function following TDD methodology.

Frequently Asked Questions about golang-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write table-driven tests in Go?

Define a slice of anonymous structs with name, inputs, and expected outputs, then loop over cases calling t.Run for each subtest. This pattern covers many scenarios with minimal code and is the standard Go testing idiom.

How to run fuzz tests in Go?

Write a function starting with Fuzz that accepts *testing.F, add seed corpus entries with f.Add, then define the fuzz function with f.Fuzz. Run it with go test -fuzz=FuzzName -fuzztime=30s; fuzzing requires Go 1.18 or later.

How do I measure Go test coverage?

Run go test -coverprofile=coverage.out ./... to generate a coverage profile, then use go tool cover -html=coverage.out for a browser view or -func for per-function percentages. Add -race to combine coverage with race detection.

What is the best way to mock dependencies in Go tests?

Define an interface for the dependency, then create a mock struct with function fields matching each method. Inject the mock into the code under test and set the function fields per test case to control behavior and simulate errors.

Why should I avoid testing private functions in Go?

Testing private functions couples tests to implementation details, making refactors break tests unnecessarily. Test through the public API instead, which verifies actual behavior and keeps tests stable during internal changes.

How do I test HTTP handlers in Go?

Use httptest.NewRequest to build a request and httptest.NewRecorder to capture the response, then call the handler directly. Assert on the recorder's status code and body, optionally using table-driven tests for multiple endpoints.