美文网首页
go test & go vet 2022-07-26

go test & go vet 2022-07-26

作者: 9_SooHyun | 来源:发表于2022-07-26 14:53 被阅读0次

    go test

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

    • first, go test recompiles each package along with any files with names matching
      the file pattern "*_test.go"
      . These additional files "*_test.go" can contain test functions, benchmark functions, and example functions. See 'go help testfunc' for more.
      这就说,go test会连带着*_test.go文件一起,编译每一个package(而go build是不会管*_test.go文件的).
      Each listed package after go test causes the execution of a separate 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.
    
    • second, 在编译package后的link阶段并行执行vet。as part of building a test binary, go test runs go vet on the package and its test source files to identify significant problems. If go vet finds any problems, go test reports those and does not run the test
      binary
      . Only a high-confidence subset of the default go vet checks are used. That subset is: 'atomic', 'bool', 'buildtags', 'errorsas', 'ifaceassert', 'nilfunc', 'printf', and 'stringintconv'. You can see the documentation for these and other vet tests via "go doc cmd/vet".
      To disable the running of go vet, use the -vet=off flag.
      'go test' start 'go vet'

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

    • third, run the test-binary with some specified flags.

    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/...

    相关文章

      网友评论

          本文标题:go test & go vet 2022-07-26

          本文链接:https://www.haomeiwen.com/subject/xnkoirtx.html