Cette page est affichée en anglais. Une traduction française est en cours.
ConfigsMay 28, 2026·3 min de lecture

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.

Prêt pour agents

Installation avec revue préalable

Cet actif nécessite une revue. Le prompt copié demande un dry-run, affiche les écritures, puis continue seulement après confirmation.

Needs Confirmation · 66/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
gotests Generator
Commande avec revue préalable
npx -y tokrepo@latest install 1e5ed972-5ad5-11f1-9bc6-00163e2b0d79 --target codex

Dry-run d'abord, confirmez les écritures, puis lancez cette commande.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires