❤️💕💕CS自学指南,大学教育无论是深度还是广度都没有办法支撑我们的职业素养,这个板块算是自己在CS学习中额外补充和记录的。个人博客:http://nsddd.top
[TOC]
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.
Uses the Sublime Text 3
official plugin, support vscode、vim、golang、IDEA
GoTests-Sublime
makes writing better Go tests easy. It is an IDE plugin for Sublime Text 3 that usesgotests
to 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
Prequisite: Use go get
to install and update the gotests
tool:
$ 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 stdout
.
$ gotests [options] PATH ...
Available options:
$ 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
tip:
-w
is followed by multiple files for which unit test files can be generated simultaneously.
in vscode:
Right-click a method in the go file and select Go:Generate Uint Tests For Function
to generate the test method for testing.
**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. Thiswant
argument 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.