侧边栏壁纸
博主头像
smile博主等级

计科专业20级学生

  • 累计撰写 50 篇文章
  • 累计创建 69 个标签
  • 累计收到 12 条评论

目 录CONTENT

文章目录

Gotests Automatically generate Go test

smile
2023-02-17 / 0 评论 / 0 点赞 / 136 阅读 / 559 字 / 正在检测是否收录...
温馨提示:
文章最后更新于 2023-02-17,若内容或图片失效,请评论反馈,我们将收到邮箱。部分素材来自网络,若不小心影响到您的利益,请联系我们删除✌️。

Gotests Automatically generate Go test

GoTests

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.

Demo

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 uses gotests to generate table driven tests from selected function and method signatures. Any new dependencies in the test files are automatically imported.

gotests

Install

Minimum Go version: Go 1.6

Prequisite: Use go get to install and update the gotests tool:

$ go get -u github.com/cweill/gotests/...

User

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

examples

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 explanation above

**The generated test case looks like this **:

  1. name: Name of the test case.
  2. args: Pass an argument to the **test function **.
  3. want: The expected value. This want argument may vary slightly depending on the function return value.
  4. 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.

0

评论区