golang-testing

Guide Go testing with TDD, table-driven tests, benchmarks, and coverage analysis.

10|3|Updated Apr 3, 2012
One-click install
npx skills add https://github.com/liuerfire/dotfiles --skill golang-testing-liuerfire
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-testing
Source: https://github.com/liuerfire/dotfiles/tree/main/agentic/skills/golang-testing
Command: npx skills add https://github.com/liuerfire/dotfiles --skill golang-testing-liuerfire

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and examples for writing robust, maintainable, and efficient tests in Go, ensuring code quality and reliability.

Core Features & Use Cases

  • TDD Workflow: Demonstrates the Red-Green-Refactor cycle for Go development.
  • Advanced Testing Techniques: Covers table-driven tests, subtests, benchmarks, fuzzing, golden files, and mocking.
  • Coverage & CI/CD: Explains how to measure test coverage and integrate testing into CI/pipelines.
  • Use Case: When developing a new Go service, use this Skill to learn and apply best practices for unit, integration, and performance testing, leading to more stable and performant code.

Quick Start

Learn the Go TDD workflow by following the Red-Green-Refactor cycle for a simple Add function.

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 and subtests in Go?

Table-driven tests in Go involve defining test cases in a struct slice and iterating over them using subtests with t.Run. This approach executes multiple scenarios cleanly while maintaining shared setup and assertion logic for maintainable test suites.

What is the best way to measure Go test coverage and integrate it with CI/CD pipelines?

Go test coverage is measured using the go test -cover flag to calculate executed code percentages. Integrate this into CI/CD pipelines by generating coverage profiles and enforcing minimum thresholds to ensure reliable code quality checks during automated builds.

How does fuzzing work in Go and when should I use it?

Go fuzzing works by automatically generating random inputs to discover edge cases and panic-triggering bugs in your code. Use fuzzing when testing complex parsing logic or functions handling untrusted input to identify unexpected runtime failures.

Can I use golden files and mocking for testing HTTP handlers in Go?

Golden files store expected HTTP response outputs for comparison against actual handler results, while mocking isolates dependencies during testing. Both techniques facilitate validating HTTP handler behavior and ensuring deterministic integration tests without relying on external services.

Does the Go TDD workflow support the Red-Green-Refactor cycle?

The Go TDD workflow fully supports the Red-Green-Refactor cycle by writing failing tests first, implementing minimal code to pass, and then refactoring. This methodology ensures code reliability and maintainability from the start of service development.

How do I run Go benchmarks to analyze code performance?

Go benchmarks are executed using the go test -bench command to measure function execution time and memory allocations. They provide detailed performance metrics to help optimize code bottlenecks and ensure efficient resource utilization.