golang-testing

Guides Go testing with TDD, table-driven tests, subtests, benchmarks, fuzzing, and coverage analysis.

Updated Jan 9, 2025
One-click install
npx skills add https://github.com/baotoq/go-shortener --skill golang-testing-baotoq
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-testing
Source: https://github.com/baotoq/go-shortener/tree/main/.agents/skills/golang-testing
Command: npx skills add https://github.com/baotoq/go-shortener --skill golang-testing-baotoq

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and examples for writing robust, reliable, and efficient tests for Go applications, ensuring code quality and maintainability.

Core Features & Use Cases

  • Test-Driven Development (TDD): Learn the RED-GREEN-REFACTOR cycle for Go.
  • Table-Driven Tests: Efficiently test multiple scenarios with clear, concise code.
  • Subtests & Parallelism: Organize tests logically and speed up execution.
  • Benchmarks: Measure and optimize code performance.
  • Fuzzing: Discover edge cases and vulnerabilities in your code.
  • Test Coverage: Understand and improve your code's test coverage.
  • HTTP Handler Testing: Easily test your web service endpoints.
  • Mocking: Isolate components using interface-based mocking.

Quick Start

Follow the TDD workflow by writing a failing test for a new Go function, then implement the minimal code to pass it.

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?

Table-driven tests in Go use a slice of structs containing input and expected output fields to iterate through scenarios, executing assertion logic in a single testing function to validate multiple cases concisely.

What is the best way to start test-driven development in Go?

To start test-driven development in Go, follow the RED-GREEN-REFACTOR cycle by writing a failing test first, implementing the minimal code required to pass that test, and then refactoring the implementation.

How do I measure code coverage in Go testing?

Measure Go test coverage by running the testing command with coverage flags to generate a profile, which identifies untested code paths and helps improve your application's overall test coverage.

Can I use Go fuzzing to discover edge cases?

Yes, Go fuzzing automatically generates random inputs to pass into your test functions, which helps discover edge cases, unexpected panics, and security vulnerabilities in your code.

How do I run subtests and parallel tests in Go?

Run subtests and parallel tests in Go by utilizing the subtest feature within the testing package, structuring tests logically with t.Run, and calling t.Parallel to speed up execution.

What is the best way to test HTTP handlers in Go?

Test HTTP handlers in Go by using the standard library's httptest package to create mock requests and response recorders, easily validating your web service endpoints without starting a server.