Gotests Automatically generate Go test
gotests: Automatically generate Go test boilerplate from your source code.
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.
Sublime Text 3 official plugin, support vscode、vim、golang、IDEA
GoTests-Sublimemakes writing better Go tests easy. It is an IDE plugin for Sublime Text 3 that uses
goteststo generate table driven tests from selected function and method signatures. Any new dependencies in the test files are automatically imported.
Minimum Go version: Go 1.6
go get to install and update the
$ go get -u github.com/cweill/gotests/...
From the commandline,
gotests can generate Go tests for specific source files or an entire directory. By default, it prints its output to
$ gotests [options] PATH ...
$ gotests --help Usage of gotests: -all generate tests for all functions and methods -excl string 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 -nosubtests disable generating tests using the Go 1.7 subtests feature -only string regexp. generate tests for functions and methods that match only. Takes precedence over -all -parallel enable generating parallel subtests -template string optional. Specify custom test code templates, e.g. testify. This can also be set via environment variable GOTESTS_TEMPLATE -template_dir string optional. 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_params string read external parameters to template by json with stdin -template_params_file string read external parameters to template by json with file -w write output to (test) files instead of stdout
Generates test methods for all functions and methods in the source file:
$gotests -all -w -i XXX.go
Generates unit tests for all functions in a file:
$gotests -all dao >> dao/dao_test
Generate a test method for a single method
$gotests -w -only ^XXX$ PATH
Generates unit tests for the specified function
$gotests -only Auth jwt.go >> jwt_test.go
-w is followed by multiple files for which unit test files can be generated simultaneously.
Right-click a method in the go file and select
Go:Generate Uint Tests For Function to generate the test method for testing.
📜 The explanation above
**The generated test case looks like this **:
name: Name of the test case.
args: Pass an argument to the **test function **.
want: The expected value. This
wantargument may vary slightly depending on the function return value.
wantErr: Whether to expect an error.
If it is a function of the test object, such as Test_tckit_Sign, there may be an extra field that needs to be filled in by itself.