home / skills / lookatitude / beluga-ai / go-testing

go-testing skill

/.claude/skills/go-testing

This skill helps you apply Go testing patterns to write robust tests, mocks, and benchmarks with clear structure and coverage.

npx playbooks add skill lookatitude/beluga-ai --skill go-testing

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
1.8 KB
---
name: go-testing
description: Go testing patterns for Beluga AI v2. Use when writing tests, mocks, or benchmarks.
---

# Go Testing Patterns

## Table-Driven Tests

```go
func TestGenerate(t *testing.T) {
    tests := []struct {
        name    string
        input   []schema.Message
        want    *schema.AIMessage
        wantErr error
    }{
        {name: "simple", input: []schema.Message{schema.HumanMessage("hello")}, want: &schema.AIMessage{...}},
        {name: "empty input errors", input: nil, wantErr: &core.Error{Code: core.ErrInvalidInput}},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := model.Generate(context.Background(), tt.input)
            if tt.wantErr != nil { require.Error(t, err); return }
            require.NoError(t, err)
            assert.Equal(t, tt.want, got)
        })
    }
}
```

## Stream + Context Cancellation

```go
func TestStreamCancellation(t *testing.T) {
    ctx, cancel := context.WithCancel(context.Background())
    count := 0
    for _, err := range model.Stream(ctx, msgs) {
        count++
        if count == 2 { cancel() }
        if err != nil { assert.ErrorIs(t, err, context.Canceled); break }
    }
    assert.LessOrEqual(t, count, 3)
}
```

## Mocks

Location: `internal/testutil/mock<package>/`. Every mock has configurable function fields + error injection + call counting.

## Rules

- `*_test.go` alongside source, not in separate dirs.
- Use `testify/assert` (non-fatal) and `testify/require` (fatal).
- Integration tests: `//go:build integration` tag, `*_integration_test.go`.
- Benchmarks: `*_bench_test.go`, `b.ReportAllocs()`, `b.RunParallel()`.
- Compile-time check: `var _ Interface = (*Impl)(nil)`.
- Always test: error paths, context cancellation for streams, nil/empty inputs.

Overview

This skill collects Go testing patterns and pragmatic conventions for Beluga AI v2 projects. It provides concrete examples for table-driven tests, stream cancellation, mocks, integration tags, and benchmarks to speed reliable test development. Use it to standardize tests, surface common pitfalls, and ensure consistent test layout and assertions.

How this skill works

The skill inspects common test scenarios and supplies short, actionable examples: table-driven unit tests, stream handling with context cancellation, and bench/test build tags. It documents mock placement and structure, preferred assertion libraries, compile-time interface checks, and mandatory test cases to cover error paths and empty inputs. Patterns are minimal and meant to be copied into *_test.go files alongside source code.

When to use it

  • Writing unit tests for models and handlers
  • Adding stream tests that must handle context cancellation
  • Creating mocks with injected errors and call counting
  • Authoring integration tests or benchmarks
  • Enforcing compile-time interface conformance

Best practices

  • Place tests in *_test.go files next to the code they exercise, not in separate directories
  • Use table-driven tests for multiple scenarios and t.Run for subtests
  • Favor testify/require for fatal assertions and testify/assert for non-fatal checks
  • Always test error paths, nil/empty inputs, and context cancellation for streams
  • Use //go:build integration for integration tests and *_integration_test.go naming
  • For benchmarks include b.ReportAllocs() and b.RunParallel() when appropriate

Example use cases

  • Unit test a Generate function with a table of inputs and expected outputs
  • Test a streaming API that must cancel correctly using context.WithCancel
  • Create a package mock with configurable function fields, error injection, and call counters under internal/testutil/mock<package>/
  • Add a compile-time interface conformance check: var _ Interface = (*Impl)(nil)
  • Write integration tests guarded by the integration build tag and separate benchmark files for performance

FAQ

Where should mocks live?

Keep mocks under internal/testutil/mock<package>/ with function fields, error injection, and counters.

What assertion libraries to use?

Use testify/require for fatal checks and testify/assert for non-fatal assertions.