go-testing-table

Table-driven test patterns for Go

⭐2stars🔀0forks📁View on GitHub🕐Updated Dec 28, 2025

When & Why to Use This Skill

This Claude skill provides standardized table-driven test patterns for Go (Golang) to enhance code reliability and maintainability. It helps developers transition from repetitive, manual testing to idiomatic Go testing structures, enabling efficient handling of multiple test cases with clear failure reporting and subtest support.

Use Cases

  • Refactoring repetitive unit tests into a clean, loop-based table structure to reduce boilerplate and improve readability.
  • Implementing comprehensive edge-case testing for functions by easily appending new input/output pairs to an existing test suite.
  • Improving debugging workflows by utilizing named subtests that pinpoint exactly which data set caused a test failure.
  • Standardizing testing practices across a Go project to ensure consistency and facilitate easier code reviews among team members.
namego-testing-table
descriptionTable-driven test patterns for Go

Table-Driven Tests

Use when you have 2+ test cases for the same function.

CORRECT

func Test_Add_Cases(t *testing.T) {
    tests := []struct {
        name string
        a    int
        b    int
        want int
    }{
        {name: "positive numbers", a: 2, b: 3, want: 5},
        {name: "negative numbers", a: -1, b: -2, want: -3},
        {name: "zero", a: 0, b: 0, want: 0},
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got := Add(tt.a, tt.b)
            if got != tt.want {
                t.Errorf("Add(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
            }
        })
    }
}

Why:

  • Single loop handles all cases
  • Easy to add new cases
  • Named cases for clear failure messages
  • Each case runs as subtest

WRONG

func Test_Add(t *testing.T) {
    if Add(2, 3) != 5 {
        t.Error("2 + 3 failed")
    }
    if Add(-1, -2) != -3 {
        t.Error("-1 + -2 failed")
    }
    if Add(0, 0) != 0 {
        t.Error("0 + 0 failed")
    }
}

Problems:

  • Repetitive code
  • First failure stops remaining tests
  • Hard to add new cases
  • No case names in output