Skip to main content

/go-test Command

This command enforces test-driven development methodology for Go code using idiomatic Go testing patterns.

What This Command Does

  1. Define Types/Interfaces: Scaffold function signatures first
  2. Write Table-Driven Tests: Create comprehensive test cases (RED)
  3. Run Tests: Verify tests fail for the right reason
  4. Implement Code: Write minimal code to pass (GREEN)
  5. Refactor: Improve while keeping tests green
  6. Check Coverage: Ensure 80%+ coverage

When to Use

Use /go-test when:
  • Implementing new Go functions
  • Adding test coverage to existing code
  • Fixing bugs (write failing test first)
  • Building critical business logic
  • Learning TDD workflow in Go

TDD Cycle

RED     → Write failing table-driven test
GREEN   → Implement minimal code to pass
REFACTOR → Improve code, tests stay green
REPEAT  → Next test case

Command Syntax

/go-test <feature description>
feature description
string
required
Description of the Go functionality to implement with TDD

Test Patterns

Table-Driven Tests

tests := []struct {
    name     string
    input    InputType
    want     OutputType
    wantErr  bool
}{
    {"case 1", input1, want1, false},
    {"case 2", input2, want2, true},
}

for _, tt := range tests {
    t.Run(tt.name, func(t *testing.T) {
        got, err := Function(tt.input)
        // assertions
    })
}

Parallel Tests

for _, tt := range tests {
    tt := tt // Capture
    t.Run(tt.name, func(t *testing.T) {
        t.Parallel()
        // test body
    })
}

Coverage Commands

# Basic coverage
go test -cover ./...

# Coverage profile
go test -coverprofile=coverage.out ./...

# View in browser
go tool cover -html=coverage.out

# With race detection
go test -race -cover ./...

Coverage Targets

Code TypeTarget
Critical business logic100%
Public APIs90%+
General code80%+
Generated codeExclude
  • Skill: skills/golang-testing/, skills/tdd-workflow/
  • Commands: /go-build, /go-review, /verify