Configs2026年5月28日·1 分钟阅读

gotests — Automatically Generate Go Table-Driven Tests

gotests generates table-driven test boilerplate for Go functions and methods. It reads your source code, creates test files with proper signatures and test table scaffolding, and integrates with VS Code, GoLand, and other editors.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
gotests Generator
先审查命令
npx -y tokrepo@latest install 1e5ed972-5ad5-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

gotests is a Go tool that automatically generates table-driven test stubs from your source functions and methods. Instead of writing repetitive test boilerplate by hand, gotests reads function signatures and produces correctly structured _test.go files with test tables ready for you to fill in test cases.

What gotests Does

  • Generates table-driven test functions from any Go function or method signature
  • Creates test files with proper package declarations and imports
  • Supports generating tests for all functions in a file or for specific named functions
  • Produces idiomatic Go test patterns following community conventions
  • Integrates with editor plugins to generate tests with a single command

Architecture Overview

gotests parses Go source files using the go/parser and go/types packages to extract function signatures, parameter types, and return types. It then applies Go templates to generate test function bodies that include a test table struct, a loop iterating over cases, and assertions. The generated code is written through go/format to ensure it is properly formatted.

Self-Hosting & Configuration

  • Install with go install github.com/cweill/gotests/gotests@latest
  • Run gotests -all -w . to generate tests for all functions and write them to _test.go files
  • Use -only "FuncName" to generate tests for specific functions by regex
  • Customize output templates by providing your own Go template files
  • Supported in VS Code (Go extension), GoLand, Vim (vim-go), and Emacs

Key Features

  • Table-driven test generation following Go best practices
  • Custom templates for project-specific test patterns
  • Works with methods, functions, and interface implementations
  • Skips functions that already have tests to avoid overwriting
  • Editor integration generates tests at cursor position

Comparison with Similar Tools

  • go test — the test runner itself; gotests generates the test code that go test runs
  • testify — assertion library; gotests generates the test structure, testify provides richer assertions
  • gotestsum — enhanced test output; gotests focuses on code generation, not test execution
  • GoMock — generates mock implementations; gotests generates test functions
  • Copilot/AI — generates tests contextually; gotests produces consistent, template-based scaffolding

FAQ

Q: Does gotests overwrite existing tests? A: No, gotests detects existing test functions and skips them to avoid overwriting your test cases.

Q: Can I customize the generated test format? A: Yes, gotests supports custom Go templates that let you control the exact shape of generated tests.

Q: Does it work with methods on structs? A: Yes, gotests handles both standalone functions and methods with receivers, including pointer receivers.

Q: How do I generate tests for a single function? A: Use gotests -only "^MyFunc$" -w ./pkg/ to target a specific function by regex pattern.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产