Top Related Projects
Go tool to modify struct field tags
impl generates method stubs for implementing an interface.
[mirror] Go Tools
Quick Overview
Gotests is a Go command-line tool that automatically generates table-driven tests based on function and method signatures. It aims to reduce the time and effort required to write unit tests for Go code, promoting better test coverage and code quality.
Pros
- Automatically generates boilerplate test code, saving time and reducing repetitive work
- Creates table-driven tests, which are considered a best practice in Go testing
- Supports generation of tests for functions, methods, and interfaces
- Integrates well with popular text editors and IDEs through plugins
Cons
- Generated tests may require manual refinement for complex scenarios
- Might not cover all edge cases or specific business logic without developer intervention
- Learning curve for customizing test generation templates
- May generate unnecessary tests for simple or self-explanatory functions
Getting Started
- Install Gotests:
go install github.com/cweill/gotests/...@latest
- Generate tests for a specific function:
gotests -only="FunctionName" /path/to/file.go
- Generate tests for all functions in a file:
gotests /path/to/file.go
- Generate tests for an entire package:
gotests ./...
For more advanced usage and options, refer to the project's README on GitHub.
Competitor Comparisons
Go tool to modify struct field tags
Pros of gomodifytags
- Focuses specifically on modifying struct field tags, offering more specialized functionality
- Provides options for adding, removing, or updating multiple tag types simultaneously
- Supports custom tag options and formatting, allowing for greater flexibility
Cons of gomodifytags
- Limited to working with struct tags, while gotests generates entire test files
- Requires more manual input and configuration compared to gotests' automated test generation
- May require additional tools or steps to achieve comprehensive test coverage
Code Comparison
gomodifytags:
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
After running gomodifytags:
type User struct {
Name string `json:"name" xml:"name"`
Age int `json:"age" xml:"age"`
}
gotests:
func TestUser_Method(t *testing.T) {
tests := []struct {
name string
u User
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.u.Method(); got != tt.want {
t.Errorf("User.Method() = %v, want %v", got, tt.want)
}
})
}
}
impl generates method stubs for implementing an interface.
Pros of impl
- Focuses specifically on generating method stubs for interfaces
- Lightweight and easy to integrate into existing workflows
- Supports generating stubs for multiple interfaces at once
Cons of impl
- Limited to interface implementation, unlike gotests' broader test generation
- Doesn't provide test case generation or scaffolding
- May require more manual work to flesh out generated method stubs
Code Comparison
impl:
type Stringer interface {
String() string
}
// Generated stub:
func (t *Type) String() string {
panic("not implemented")
}
gotests:
func TestString(t *testing.T) {
tests := []struct {
name string
t *Type
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.t.String(); got != tt.want {
t.Errorf("Type.String() = %v, want %v", got, tt.want)
}
})
}
}
impl generates minimal method stubs, while gotests creates comprehensive test scaffolding with table-driven tests.
[mirror] Go Tools
Pros of tools
- Comprehensive suite of development tools for Go
- Official Go project with extensive community support
- Includes advanced features like static analysis and code generation
Cons of tools
- Larger, more complex project with steeper learning curve
- May include unnecessary tools for simple test generation tasks
- Requires more setup and configuration for specific use cases
Code comparison
tools:
import (
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
gotests:
import (
"github.com/cweill/gotests/internal/input"
"github.com/cweill/gotests/internal/render"
"github.com/cweill/gotests/internal/output"
)
Summary
tools is a comprehensive suite of Go development tools, offering a wide range of functionality beyond test generation. It's well-maintained and supported by the Go community but may be overkill for simple test creation tasks.
gotests is focused specifically on generating table-driven tests for Go functions. It's simpler to use and more lightweight, making it ideal for developers primarily interested in quick and easy test generation.
Choose tools for a full-featured development environment or gotests for streamlined test creation in Go projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
gotests
gotests
makes writing Go tests easy. It's a Golang commandline tool that generates table driven tests based on its target source files' function and method signatures. Any new dependencies in the test files are automatically imported.
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.6
Use go get
to install and update:
$ go get -u github.com/cweill/gotests/...
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
.
$ gotests [options] PATH ...
Available options:
-all generate tests for all functions and methods
-excl regexp. generate tests for functions and methods that don't
match. Takes precedence over -only, -exported, and -all
-exported generate tests for exported functions and methods. Takes
precedence over -only and -all
-i print test inputs in error messages
-only regexp. generate tests for functions and methods that match only.
Takes precedence over -all
-nosubtests disable subtest generation when >= Go 1.7
-parallel enable parallel subtest generation when >= Go 1.7.
-w write output to (test) files instead of stdout
-template_dir Path to a directory containing custom test code templates. Takes
precedence over -template. This can also be set via environment
variable GOTESTS_TEMPLATE_DIR
-template Specify custom test code templates, e.g. testify. This can also
be set via environment variable GOTESTS_TEMPLATE
-template_params_file read external parameters to template by json with file
-template_params read external parameters to template by json with stdin
Contributions
Contributing guidelines are in CONTRIBUTING.md.
License
gotests
is released under the Apache 2.0 License.
Top Related Projects
Go tool to modify struct field tags
impl generates method stubs for implementing an interface.
[mirror] Go Tools
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot