在根项目文件夹中运行Go测试。

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

Running go tests from root project folder

问题

我必须接手一个Go项目,但我以前从未接触过Go,并且该项目没有测试。所以我开始添加测试,但是我无法运行它们。

运行Go测试的命令是go test,但似乎无法在根目录下运行。

我的项目结构如下:

/project/main.go
/project/engine/*.go
/project/api/*.go

如果我进入engineapi目录,并运行go test,它可以正常工作,但在根目录下不行。

cd /project
go test

 ?   	/project	[no test files]

有没有办法从根目录运行所有的测试?

英文:

I have to take over a go project from a colleague and I've never touched Go before and it doesn't have tests so I've started to add them but I cannot run them.

The go command for running tests is go test but it doesn't seem to run at the root level.

My project structure is:

/project/main.go
/project/engine/*.go
/project/api/*.go

If I cd into either engine or api, and run go test it works but not in the root folder.

cd /project
go test

 ?   	/project	[no test files]

Is there anyway to run all tests from the root?

答案1

得分: 6

你可以使用 ...(省略号)运算符来测试当前包的所有子包。像这样:go test ./...

如果你要做更复杂的操作,还有其他解决方案,比如使用 list 工具。像这样(例如):go test $(go list ./... | grep [regex])。这对于排除 vendor 目录中的测试非常有用。

另外,你可能想了解一下 gt 命令,可以在这里找到:https://godoc.org/rsc.io/gt,并将缓存添加到 go test 命令中。

最后,不要犹豫阅读 https://blog.golang.org/cover,以获取有关在 Go 语言中进行代码覆盖分析的信息。

英文:

You can use the ... (ellipsis) operator to test all subpackages of the current package. Like that: go test ./....

There are other solutions that you might want to try later if you do something more sophisticated, like using the list tool. Like that (for example): go test $(go list ./... | grep [regex]). That's useful to exclude the vendor directory from your tests.

Another thing you may want to know about is the gt command that can be found here https://godoc.org/rsc.io/gt and add caching to the go test command.

Finally, don't hesitate to read https://blog.golang.org/cover to get informations about code-coverage analysis in golang.

答案2

得分: 1

我发现这个问题很烦人,当在项目根目录下运行go test ./...时,实际上会从pkg/pkg_name/文件夹运行。你可以通过在init()辅助函数中设置cwd来将测试设置为项目根目录。

我在这里找到了解决方案。你可以尝试在你的文件中使用以下代码片段:

func init() {
	_, filename, _, _ := runtime.Caller(0)
	dir := path.Join(path.Dir(filename), "../..") // 根据测试文件的位置进行更改
	err := os.Chdir(dir)
	if err != nil {
		panic(err)
	}
}

上面的代码片段适用于我在pkg/$name/$name_test.go中的测试。

英文:

I find this irritating that go test ./... when run from project root will actually run from pkg/pkg_name/ folder. There is a way you can set the tests to project root by setting cwd in the init() helper function.

I found the solution here You could try the following snippet in your _file:

func init() {
	_, filename, _, _ := runtime.Caller(0)
	dir := path.Join(path.Dir(filename), "../..") // change to suit test file location
	err := os.Chdir(dir)
	if err != nil {
		panic(err)
	}
}

This snippet above works for my tests in pkg/$name/$name_test.go.

答案3

得分: 0

只运行sudo go test -v ./...可能会失败,因为你可能没有为GO设置根环境,就像你设置非根环境一样。

你可以去重置根目录中的所有内容,或者执行以下简单步骤:

sudo -E go test -v ./...

这将以根用户身份运行,但保持你的环境设置。

英文:

just running sudo go test -v ./... will probably fail because you probably do not have your root environment setup for GO just like you set the non-root env.

You can go and reset everything in the root, or do this easy step:

sudo -E go test -v ./...

This will run as root but keep your env settings.

huangapple
  • 本文由 发表于 2016年4月19日 21:18:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/36719661.html
匿名

发表评论

匿名网友

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

确定