golang-testing

Write and review production-grade Go tests using table-driven patterns, testify, fuzzing, and goleak.

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-testing-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-testing
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-testing
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-testing-titaneric

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires gotests, go.uber.org/goleak, github.com/stretchr/testify, github.com/jonboulle/clockwork, and includes references (resource) components.

What problem does it solve? Writing reliable Go tests requires many idiomatic decisions—table-driven structure, parallel subtests, build-tagged integration tests, goroutine leak detection, and proper mocking—that are easy to get wrong, producing flaky, slow, or brittle test suites. ## Core Features & Use Cases - Test Authoring Guidance: Enforces table-driven tests with named subtests, t.Parallel() usage, t.Helper() attribution, and black-box testing via external test packages. - Concurrency & Time Testing: Covers goroutine leak detection with goleak, deterministic testing with testing/synctest, and fake clock injection with clockwork. - Integration & HTTP Testing: Provides patterns for httptest.NewRecorder handler tests, testify/suite integration suites with Docker Compose fixtures, and //go:build integration tags. - Use Case: When reviewing a PR that adds a worker pool, use this Skill to generate tests that verify Stop() cleans up goroutines via goleak.VerifyNone instead of only checking task completion. ## Quick Start Ask the agent to write comprehensive table-driven tests with goroutine leak detection for your Go package.

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 a name field plus inputs and expected outputs, then loop with t.Run(tt.name, func(t *testing.T)) for each case. Every case must have a descriptive name so failures identify the exact scenario.

How do I separate integration tests from unit tests in Go?

Use a //go:build integration build tag at the top of the test file, before the package declaration, and run them with go test -tags=integration ./.... Avoid testing.Short() since those tests still compile and may attempt connections in normal runs.

How do I detect goroutine leaks in Go tests?

Use go.uber.org/goleak by calling goleak.VerifyTestMain(m) in TestMain or defer goleak.VerifyNone(t) in individual tests. This fails the test if goroutines spawned during the test are still running at the end.

Should I use testing.Short() or build tags for integration tests?

Build tags are preferred because testing.Short() still compiles the tests into the binary and requires a flag to skip them, while build-tagged files are completely excluded from compilation unless -tags=integration is passed.

How do I test time-dependent Go code without slow sleeps?

Refactor the code to accept a clock interface and inject clockwork.NewFakeClock() in tests, advancing time with fakeClock.Advance(). For Go 1.25+, testing/synctest also provides deterministic synthetic time for goroutines and timers.

Why do my Go test failures point to the wrong line number?

The helper function is missing t.Helper() as its first statement. Adding it marks the function as a test helper so the testing framework reports the caller's file and line instead of the helper's internal line.