Writing Tests with gt Library

VerifiedSafe

Provides step-by-step guidance for writing Go tests with the gt library, including type-specific assertions (e.g., `gt.Array`, `gt.String`), fail-fast patterns with `Required()`, and return value handling via `R1`/`R2`/`R3`. Helps when creating new test files or adding tests to existing Go code by referencing the locally installed gt documentation.

Sby Skills Guide Bot
TestingIntermediate
1606/2/2026
Claude Code
#go#testing#gt-library#assertions#unit-testing

Recommended for

Our review

Write Go tests using the gt library for type-safe assertions.

Strengths

  • Type-safe assertions for slices, strings, errors, etc.
  • Fail-fast with Required() for immediate stop on failure
  • Context with Describef() for clear error messages
  • Simplified handling of function returns with R1/R2/R3

Limitations

  • Requires the gt library to be installed locally
  • May not cover all edge cases
  • Can be overkill for simple tests where the standard testing package is enough
When to use it

When writing Go tests and you want type-safe, readable assertions with fast failure and debugging context.

When not to use it

If the gt library is not available or for trivial tests that don't need complex assertions.

Security analysis

Safe
Quality score92/100

The skill uses only safe Bash commands (go list and file reads) restricted to the 'go' command scope. There are no destructive, exfiltration, or obfuscation operations.

No concerns found

Examples

Test array length with gt
Write a Go test for the function GetUsers that returns a slice of users. Use gt.Array to assert the length is 3.
Test string content with gt
Add a test for ParseMessage that checks the returned string contains 'hello' using gt.String.
Test error handling with Required()
Write a test for OpenFile that uses gt.NoError with Required() to fail fast if an error occurs.

name: test-with-gt description: Write Go test code using the gt library. Use when writing tests, creating test files, or when the user asks to add tests for Go code. allowed-tools: Read, Bash(go:*)

Writing Tests with gt

When writing Go test code, use the gt library for type-safe assertions.

Step 1: Locate gt documentation

First, find where gt is installed and read its documentation:

GT_DIR=$(go list -m -f '{{.Dir}}' github.com/m-mizutani/gt 2>/dev/null)

If gt is available, read the relevant documentation from $GT_DIR/docs/:

  • docs/README.md - Overview and quick reference
  • docs/types/*.md - Detailed documentation for each test type
  • docs/patterns/*.md - Common patterns (method chaining, Required, Describe)

Step 2: Choose the right test type

| Data Type | Use | NOT | |-----------|-----|-----| | []T (slice) | gt.Array(t, arr).Length(3) | gt.Value(t, len(arr)).Equal(3) | | string | gt.String(t, s).Contains("x") | gt.Bool(t, strings.Contains(s, "x")).True() | | error | gt.NoError(t, err) | gt.Value(t, err).Nil() | | int, float, etc. | gt.Number(t, n).Greater(5) | gt.Bool(t, n > 5).True() | | map[K]V | gt.Map(t, m).HasKey("k") | manual check with gt.Bool | | bool | gt.Bool(t, b).True() | gt.Value(t, b).Equal(true) |

Step 3: Apply common patterns

Use Required() for fail-fast

gt.NoError(t, err).Required()  // Stop immediately if error
gt.Value(t, result).NotNil().Required()  // Stop if nil

Use Describef() for context

gt.Array(t, users).
    Describef("Users for tenant %s", tenantID).
    Length(5)

Handle function returns with R1/R2/R3

result := gt.R1(parseJSON(input)).NoError(t)
gt.String(t, result.Name).Equal("Alice")

Quick Reference

Constructors: gt.Value, gt.Array, gt.Map, gt.Number, gt.String, gt.Bool, gt.Error, gt.NoError, gt.File, gt.Cast, gt.R1/R2/R3

Short aliases: gt.V, gt.A, gt.M, gt.N, gt.S, gt.B, gt.F, gt.C

Related skills