golang-testing

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

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill golang-testing-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-testing
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/golang-testing
Command: npx skills add https://github.com/Femad-6/my-skills --skill golang-testing-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing thorough, 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 guidance so you write correct Go tests 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. - Mocking & HTTP Testing: Interface-based mocking patterns and httptest-based handler testing for API endpoints. - Use Case: When adding a new Go function, follow the RED-GREEN-REFACTOR workflow to write a failing table-driven test first, implement minimal code, then verify with go test -race -cover. ## Quick Start Ask the AI to write table-driven tests with benchmarks and coverage for your Go package 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 it calling t.Run for each case. This pattern covers many scenarios with minimal code and works with error cases via a wantErr field.

How to run benchmarks and measure memory allocations in Go?

Write functions starting with Benchmark that loop b.N times, then run go test -bench=. -benchmem. Use b.ResetTimer() after setup and b.Run for sub-benchmarks across different input sizes.

Does Go support fuzz testing natively?

Yes, Go 1.18+ includes built-in fuzzing via testing.F. Add seed inputs with f.Add, define the fuzz function with f.Fuzz, and run with go test -fuzz=FuzzName -fuzztime=30s.

How do I mock dependencies in Go tests?

Define interfaces for dependencies, then create mock structs with function fields matching each method. Inject the mock into the code under test and set the function fields to return test-specific values or 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 behavior rather than internal structure.

How do I check Go test coverage in CI?

Run go test -coverprofile=coverage.out ./... then use go tool cover -func=coverage.out to view per-function coverage. Parse the total percentage in CI scripts to fail builds below a threshold like 80%.