agentsclimarketplace

Go testing

Skill cxuu/golang-skills/skills/go-testing

Use when writing, reviewing, or improving Go test code — including table-driven tests, subtests, parallel tests, test helpers, test doubles, and assertions with cmp.Diff. Also use when a user asks to write a test for a Go function, even if they don't mention specific patterns like table-driven tests or subtests. Does not cover benchmark performance testing (see go-performance).From its SKILL.md

Install
npx -y skills add cxuu/golang-skills --skill go-testing

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • runs commandsInstructs the agent to run 1 command, including `go test -run TestXxx -v`.

SKILL.md

5.2 KB, ~1.2k tokens by cl100k_base, as published. Nobody here has run it

Go Testing

Compatibility: Diff examples may use github.com/google/go-cmp.

Resource Routing

  • scripts/gen-table-test.sh - Run when generating a table-driven test scaffold.
  • assets/table-test-template.go - Use as a copyable table-test starting point.
  • references/TABLE-DRIVEN-TESTS.md - Read when choosing table tests, subtests, or parallel test patterns.
  • references/TEST-HELPERS.md - Read when writing helpers, fixtures, cleanup, or test doubles.
  • references/TEST-ORGANIZATION.md - Read when structuring packages, black-box tests, or larger test suites.
  • references/VALIDATION-APIS.md - Read when choosing t.Error, t.Fatal, cmp.Diff, or assertion style.
  • references/INTEGRATION.md - Read when testing external services, HTTP handlers, databases, or long-running setup.

Quick Reference

PatternUse When
t.ErrorDefault — report failure, keep running
t.FatalSetup failed or continuing is meaningless
cmp.DiffComparing structs, slices, maps, protos
Table-drivenMany cases share identical logic
SubtestsNeed filtering, parallel execution, or naming
t.Helper()Any test helper function (call as first statement)
t.Cleanup()Teardown in helpers instead of defer

Useful Test Failures

Normative: Test failures must be diagnosable without reading the test source.

Every failure message must include: function name, inputs, actual (got), and expected (want). Use the format YourFunc(%v) = %v, want %v.

// Good:
t.Errorf("Add(2, 3) = %d, want %d", got, 5)

// Bad: Missing function name and inputs
t.Errorf("got %d, want %d", got, 5)

Always print got before want: got %v, want %v — never reversed.


No Assertion Libraries

Normative: Do not use assertion libraries. Use cmp.Diff for complex comparisons.

if diff := cmp.Diff(want, got); diff != "" {
    t.Errorf("GetPost() mismatch (-want +got):\n%s", diff)
}

For protocol buffers, add protocmp.Transform() as a cmp option. Always include the direction key (-want +got) in diff messages. Avoid comparing JSON/serialized output — compare semantically instead.


t.Error vs t.Fatal

Normative: Use t.Error by default to report all failures in one run. Use t.Fatal only when continuing is impossible.

Choose t.Fatal when:

  • Setup fails (DB connection, file load)
  • The next assertion depends on the previous one succeeding (e.g., decode after encode)

Never call t.Fatal/t.FailNow from a goroutine other than the test goroutine — use t.Error instead.


Table-Driven Tests

See assets/table-test-template.go when scaffolding a new table-driven test and need the canonical struct, loop, and subtest layout.

Advisory: Use table-driven tests when many cases share identical logic.

Use table tests when: all cases run the same code path with no conditional setup, mocking, or assertions. A single shouldErr bool is acceptable.

Don't use table tests when: cases need complex setup, conditional mocking, or multiple branches — write separate test functions instead.

Key rules:

  • Use field names when cases span many lines or have same-type adjacent fields
  • Include inputs in failure messages — never identify rows by index

Validation: After generating or modifying tests, run go test -run TestXxx -v to verify the tests compile and pass. Fix any compilation errors before proceeding.


Test Helpers

Normative: Test helpers must call t.Helper() first and use t.Cleanup() for teardown.

func setupTestDB(t *testing.T) *sql.DB {
    t.Helper()
    db, err := sql.Open("sqlite3", ":memory:")
    if err != nil {
        t.Fatalf("Could not open database: %v", err)
    }
    t.Cleanup(func() { db.Close() })
    return db
}

Test Error Semantics

Advisory: Test error semantics, not error message strings.

// Bad: Brittle string comparison
if err.Error() != "invalid input" { ... }

// Good: Semantic check
if !errors.Is(err, ErrInvalidInput) { ... }

For simple presence checks when specific semantics don't matter:

if gotErr := err != nil; gotErr != tt.wantErr {
    t.Errorf("f(%v) error = %v, want error presence = %t", tt.input, err, tt.wantErr)
}

Related Skills

  • Error testing: See go-error-handling when testing error semantics with errors.Is/errors.As or sentinel errors
  • Interface mocking: See go-interfaces when creating test doubles by implementing interfaces at the consumer side
  • Naming test functions: See go-naming when naming test functions, subtests, or test helper utilities
  • Linter integration: See go-linting when running linters alongside tests in CI or pre-commit hooks

What ships with it: 7 files

24.8 KB alongside SKILL.md, 1 of them executable

scripts/

Gives 0 of the 12 instructions most docs writing skills give in ~1.2k tokens

Counted across 1,951 of the 3,904 authors here whose files we hold, read 2026-09-06

  • Use third-person for skill descriptionsin 54 of 1951, across 35 files
  • Start descriptions with Use whenin 43 of 1951, across 29 files
  • Run baseline scenarios before writing any skillin 40 of 1951, across 26 files
  • Use active voicein 40 of 1951, across 36 files
  • Map file responsibilities before defining tasksin 36 of 1951, across 29 files
  • Use checkbox syntax for tracking stepsin 35 of 1951, across 27 files
  • Ask one question at a timein 35 of 1951
  • Offer execution options after saving the planin 33 of 1951, across 24 files
  • Include complete code in every stepin 33 of 1951, across 27 files
  • Design units with clear boundaries and interfacesin 31 of 1951, across 23 files
  • Announce the skill usage at the startin 30 of 1951
  • Verify agent compliance after adding the skillin 29 of 1951, across 17 files

Said here and by no other author read

  • use t.Error by default to report failures
  • use t.Fatal only when continuing is impossible
  • include function name and inputs in failure messages
  • print got before want in failure messages
  • use cmp.Diff for complex comparisons
  • call t.Helper as the first statement in helpers

Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.