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.gofiles - 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.