如何在我的项目中运行`go test`命令来测试所有的测试?

huangapple go评论89阅读模式
英文:

How to `go test` all tests in my project?

问题

go test命令只会覆盖一个目录中的*_test.go文件。

我想要对整个项目进行go test,也就是说测试应该覆盖目录./下的所有*_test.go文件以及目录./下的每个子目录。

如何使用命令来实现这个目标?

英文:

The go test command covers *_test.go files in only one dir.

I want to go test the whole project, which means the test should cover all *_test.go files in the dir ./ and every children tree dir under the dir ./.

What's the command to do this?

答案1

得分: 543

这应该在当前目录及其所有子目录中运行所有测试:

$ go test ./...

这应该运行给定特定目录中的所有测试:

$ go test ./tests/... ./unit-tests/... ./my-packages/...

这应该运行以foo/为前缀的所有导入路径的测试:

$ go test foo/...

这应该运行以foo为前缀的所有导入路径的测试:

$ go test foo...

这应该运行您的$GOPATH中的所有测试:

$ go test ...
英文:

This should run all tests in current directory and all of its subdirectories:

$ go test ./...

This should run all tests for given specific directories:

$ go test ./tests/... ./unit-tests/... ./my-packages/...

This should run all tests with import path prefixed with foo/:

$ go test foo/...

This should run all tests import path prefixed with foo:

$ go test foo...

This should run all tests in your $GOPATH:

$ go test ...

答案2

得分: 81

从Go 1.9开始,使用

go test ./...

在Go 1.6到1.8版本中,./...也会匹配vendor目录。要跳过供应商包,可以使用

go test $(go list ./... | grep -v /vendor/)

来源:https://github.com/golang/go/issues/11659, https://github.com/golang/go/issues/14417, https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2366, @nickgrim的评论。

英文:

From Go 1.9 onwards, use

go test ./...

In Go 1.6 through 1.8, the ./... matched also the vendor directory. To skip vendored packages, you'd use

go test $(go list ./... | grep -v /vendor/)

Sources: https://github.com/golang/go/issues/11659, https://github.com/golang/go/issues/14417, https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2366, @nickgrim's comment.

答案3

得分: 16

>文件夹结构

项目名/文件夹名1/文件_test.go
项目名/文件夹名2/文件1_test.go
项目名/文件夹名3/文件2_test.go

>go test命令

项目名$ go test -v ./...
项目名$ go test  ./...
项目名$ go test -cover ./...

>整个项目的覆盖率报告

ok   	项目名/文件夹名1 10%
ok   	项目名/文件夹名2	90%
ok      项目名/文件夹名3	85%
英文:

>Folder Structure

ProjectName/folderName1/file_test.go
ProjectName/folderName2/file1_test.go
ProjectName/folderName3/file2_test.go

>go test command Command

   ProjectName$ go test -v ./...
    ProjectName$ go test  ./...
    ProjectName$ go test -cover ./...

>Coverage Report for the Entire Project

ok   	ProjectName/folderName1 10%
ok   	ProjectName/folerName2	90%
ok      ProjectName/folerName2	85%

huangapple
  • 本文由 发表于 2013年5月3日 14:33:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/16353016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定