Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
G

gotests

> 测试质量
Open source

Automatically generate Go test boilerplate from your source code.

5.3K stars0 likes3 views
WebsiteGitHub

About

Automatically generate Go test boilerplate from your source code.

gotests

gotests is a powerful Go test generator that automatically creates table-driven tests from your source code. It analyzes function and method signatures to generate comprehensive test scaffolding, saving you time and ensuring consistency across your test suite.

Key Features

  • Zero-config test generation - Works out of the box with any Go project
  • Smart test scaffolding - Generates complete table-driven test structure with proper type handling
  • Flexible filtering - Generate tests for specific functions, exported functions, or entire packages
  • Auto-imports - Automatically adds required imports to test files
  • Full generics support - Works seamlessly with Go 1.18+ type parameters
  • Recursive generation - Process entire directory trees with ./... pattern
  • Custom templates - Built-in support for testify and custom test templates
  • ⚡ Parallel subtests - Optional parallel test execution support

Demo

The following shows gotests in action using the official Sublime Text 3 plugin. Plugins also exist for Emacs, also Emacs, Vim, Atom Editor, Visual Studio Code, and IntelliJ Goland.

Installation

Minimum Go version: Go 1.22

Use go install to install and update:

bash
$ go install github.com/cweill/gotests/gotests@latest

Usage

From the commandline, gotests can generate Go tests for specific source files or an entire directory. By default, it prints its output to stdout.

bash
$ gotests [options] PATH ...

Available options:

…

AI-Powered Test Generation

gotests can generate intelligent test cases using local LLMs via Ollama. This feature analyzes your function implementations and generates realistic test values, edge cases, and error conditions.

Quick Start

  1. Install Ollama (https://ollama.ai)

  2. Pull a model:

    bash
    ollama pull qwen2.5-coder:0.5b  # Small, fast model (400MB)
    # or
    ollama pull llama3.2:latest     # Larger, more capable (2GB)
  3. Generate tests with AI:

    bash
    gotests -all -ai -w yourfile.go

Example

Given this function:

go
func CalculateDiscount(price float64, percentage int) (float64, error) {
    if price < 0 {
        return 0, errors.New("price cannot be negative")
    }
    if percentage < 0 || percentage > 100 {
        return 0, errors.New("percentage must be between 0 and 100")
    }
    discount := price * float64(percentage) / 100.0
    return price - discount, nil
}

The AI generates (showing 3 cases; by default, the AI generates between 3-10 cases):

…

AI Options

bash
# Use a different model
gotests -all -ai -ai-model llama3.2:latest -w yourfile.go

# Generate a specific number of test cases (min = max)
gotests -all -ai -ai-min-cases 5 -ai-max-cases 5 -w yourfile.go

# Generate a range of test cases (AI chooses between 3-7)
gotests -all -ai -ai-min-cases 3 -ai-max-cases 7 -w yourfile.go

# Combine with other flags
gotests -exported -ai -parallel -w yourfile.go

How It Works

  • Analyzes function implementation and logic
  • Generates realistic test values based on actual code
  • Creates test cases for edge cases and error conditions
  • Falls back to TODO comments if generation fails
  • Works offline with local models (privacy-first)

Supported Features

✅ Simple types (int, string, bool, float) ✅ Complex types (slices, maps, structs, pointers) ✅ Error returns and validation ✅ Variadic parameters ✅ Methods with receivers ✅ Multiple return values

Privacy & Security

What data is sent to the LLM:

  • Function signatures (name, parameters, return types)
  • Complete function bodies including all code and comments
  • No file paths or project context

Privacy considerations:

  • ⚠️ Function bodies may contain sensitive information - business logic, algorithms, or credentials/secrets in comments
  • ✅ Local-first by default - Using Ollama keeps all data on your machine; nothing is sent to external servers
  • ✅ Offline operation - AI generation works completely offline with local models
  • Recommendation: Avoid using -ai on code containing secrets, API keys, or proprietary algorithms in comments

If using cloud providers in the future:

  • Function source code will be transmitted to the cloud provider's API
  • Review the provider's data retention and privacy policies
  • Consider using -ai only on non-sensitive codebases

Quick Start Examples

Generate tests for a single function

Given a file math.go:

go
package math

func Add(a, b int) int {
    return a + b
}

Generate a test:

bash
$ gotests -only Add -w math.go

This creates math_test.go with:

go
func TestAdd(t *testing.T) {
    type args struct {
        a int
        b int
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        // TODO: Add test cases.
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := Add(tt.args.a, tt.args.b); got != tt.want {
                t.Errorf("Add() = %v, want %v", got, tt.want)
            }
        })
    }
}

Generate tests for all exported functions

bash
$ gotests -all -exported -w .

This generates tests for all exported functions in the current directory.

Generate tests recursively

bash
$ gotests -all -w ./...

This generates tests for all functions in the current directory and all subdirectories.

Generate tests with testify

bash
$ gotests -all -template testify -w calculator.go

This generates tests using the testify assertion library.

Common workflows

bash
# Generate tests for all exported functions in a package
$ gotests -exported -w pkg/*.go

# Generate only tests for specific functions matching a pattern
$ gotests -only "^Process" -w handler.go

# Generate tests excluding certain functions
$ gotests -all -excl "^helper" -w utils.go

# Generate parallel subtests
$ gotests -all -parallel -w service.go

Go Generics Support

gotests fully supports Go generics (type parameters) introduced in Go 1.18+. It automatically generates tests for generic functions and methods on generic types.

Example: Generic Function

Given this generic function:

go
func FindFirst[T comparable](slice []T, target T) (int, error) {
    for i, v := range slice {
        if v == target {
            return i, nil
        }
    }
    return -1, ErrNotFound
}

Running gotests -all -w yourfile.go generates:

…

Example: Methods on Generic Types

gotests also supports methods on generic types:

go
type Set[T comparable] struct {
    m map[T]struct{}
}

func (s *Set[T]) Add(v T) {
    if s.m == nil {
        s.m = make(map[T]struct{})
    }
    s.m[v] = struct{}{}
}

Generates:

go
func TestSet_Add(t *testing.T) {
    type args struct {
        v string
    }
    tests := []struct {
        name string
        s    *Set[string]
        args args
    }{
        // TODO: Add test cases.
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            s := &Set[string]{}
            s.Add(tt.args.v)
        })
    }
}

Type Constraint Mapping

gotests uses intelligent defaults for type parameter instantiation:

  • any → int
  • comparable → string
  • Union types (int64 | float64) → first option (int64)
  • Approximation constraints (~int) → underlying type (int)

This ensures generated tests use appropriate concrete types for testing generic code.

Contributions

Contributing guidelines are in CONTRIBUTING.md.

License

gotests is released under the Apache 2.0 License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gocode-generatorcommandlinegogo-test

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category测试质量
PricingOpen source

> Related tools

J
Jest
JavaScript 测试框架
P
Playwright
现代端到端测试框架
E
eslint-plugin-test-selectors
Enforces that data-test-id attributes are added to interactive DOM elements (JSX) to help with UI testing. JSX only.