go test & go vet 2022-07-26

2022-07-26  本文已影响0人  9_SooHyun

go test

in summary, [go test = compile, vet while link, and run test-binary]

Go test runs in two different modes:

The first, called local directory mode, occurs when go test is
invoked with no package arguments (for example, 'go test' or 'go
test -v'). In this mode, go test compiles the package sources and
tests found in the current directory and then runs the resulting
test binary. In this mode, caching (discussed below) is disabled.
After the package test finishes, go test prints a summary line
showing the test status ('ok' or 'FAIL'), package name, and elapsed
time.

The second, called package list mode, occurs when go test is invoked
with explicit package arguments (for example 'go test math', 'go
test ./...', and even 'go test .'). In this mode, go test compiles
and tests each of the packages listed on the command line. If a
package test passes, go test prints only the final 'ok' summary
line. If a package test fails, go test prints the full test output.
If invoked with the -bench or -v flag, go test prints the full
output even for passing package tests, in order to display the
requested benchmark results or verbose logging. After the package
tests for all of the listed packages finish, and their output is
printed, go test prints a final 'FAIL' status if any package test
has failed.

另外,这里记录了把go vet引入go test的讨论 https://github.com/golang/go/issues/18084

type go help test and go help testfunc for detail information

usage: go test [build/test flags] [packages] [build/test flags & test binary flags]

- build flags: 编译test binary的命令行选项, such as -c which means: Compile the test binary to $pkgName.test but do not run it
For more about build flags, see 'go help build'.

- test flags: control the execution of any test, such as -run regexp: 仅运行与正则表达式匹配的测试和示例. 
eg. go test ./service/my_test.go -v -run TestMyLog # 在my_test.go中执行与TestMyLog匹配的测试用例

- test binary flags: 测试文件自定义的flags

go vet

vet in Chinese is 兽医
Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.
Vet is normally invoked through the go command. This command vets the package in the current directory:
go vet
whereas this one vets the packages whose path is provided:
go vet my/project/...

上一篇下一篇

猜你喜欢

热点阅读