Get the FREE Ultimate OpenClaw Setup Guide →

go-test

npx machina-cli add skill geekswamp/agent-skills/go-test --openclaw
Files (1)
SKILL.md
2.1 KB

Generate Go unit tests for the provided code using Go testing best practices.

Requirements

  • Use table-driven tests for all testable functions.
  • Use mocking where appropriate:
    • Prefer testify/mock for interface dependencies.
    • Avoid real external dependencies (DB, network, filesystem).
  • Focus on test coverage, including:
    • Success cases
    • Error cases
    • Edge cases
    • Boundary conditions
  • Tests must be deterministic and translation-safe (no reliance on time, randomness, or global state unless mocked).
  • Follow idiomatic Go conventions:
    • File name: <file>_test.go
    • Test name: Test<FunctionName>
  • Always use English in test names and comments.
  • Enforce minimum package coverage of ≥80%.
  • Coverage must include:
    • At least one error path per public function (if applicable).
    • Boundary or edge cases where reasonable.

Output Expectations

  • Generate complete, compilable test code.
  • Include necessary imports.
  • Clearly separate:
    • Arrange
    • Act
    • Assert
  • Prefer readability to cleverness.
  • Generated tests should realistically achieve ≥80% coverage when executed with: go test -cover ./...

Additional Guidelines

  • Prefer small, focused tests over large monolithic ones.
  • If a function is hard to test:
    • Explain briefly why
    • Suggest a refactor (e.g., dependency injection).
  • Do not test private implementation details—test behavior.
  • Avoid snapshot-style assertions unless explicitly requested.
  • If achieving ≥ 80% coverage is not possible without testing private implementation details:
    • Explain the limitation briefly
    • Suggest a refactor (interface extraction, dependency injection)
  • Avoid fake coverage (e.g. meaningless assertions).

Please Read the References

Source

git clone https://github.com/geekswamp/agent-skills/blob/main/skills/go-test/SKILL.mdView on GitHub

Overview

Generates deterministic Go unit tests for provided code using testutil helpers, table-driven patterns, and infra mocks. Emphasizes achieving ≥80% package coverage while avoiding real I/O dependencies.

How This Skill Works

The tool scans Go packages, scaffolds tests in <filename>_test.go, and creates table-driven cases for exported functions. It uses testify/mock to mock interfaces and testutil style helpers to assert outcomes, avoiding real DB, network, or filesystem access. Determinism is ensured by injecting fixed inputs and mocks, so tests produce consistent results and measurable coverage.

When to Use It

  • Testing Go packages with interfaces and external dependencies (DB, HTTP, file I/O)
  • Refactoring to extract dependencies and improve testability
  • Enforcing table-driven testing patterns across functions
  • Building tests that exercise success, error, edge, and boundary scenarios
  • Setting up English, idiomatic Test naming and deterministic tests

Quick Start

  1. Step 1: Add dependencies for testing, e.g., github.com/stretchr/testify and testutil helpers
  2. Step 2: Create <file>_test.go and implement a table-driven Test<FunctionName> with mocks
  3. Step 3: Run go test -cover ./... and iterate toward ≥80% coverage

Best Practices

  • Use table-driven tests for every public function
  • Mock external dependencies with testify/mock; avoid real IO
  • Include success, error, edge, and boundary cases
  • Inject mocks and fixed inputs to guarantee determinism
  • Aim for ≥80% coverage and verify with go test -cover

Example Use Cases

  • TestParseConfig validates config parsing with valid input and error on invalid JSON
  • TestUserService_CreateUser uses a mocked repository to test success and failure paths
  • TestCalculateDiscount covers zero, negative, and large values
  • TestFetchRemoteData uses a mocked HTTP client to simulate responses
  • TestValidateInput tests empty, whitespace, and max length inputs

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers