Skip to content

Latest commit

 

History

History
147 lines (88 loc) · 4.38 KB

File metadata and controls

147 lines (88 loc) · 4.38 KB

第46节 Gotests Automatically generate Go test

❤️💕💕CS自学指南,大学教育无论是深度还是广度都没有办法支撑我们的职业素养,这个板块算是自己在CS学习中额外补充和记录的。个人博客:http://nsddd.top


[TOC]

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.

go-test012f

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.

END Links