当测试文件在模块中时,如何运行`go test`? [英] How do you run `go test` when test files are within a module?
问题描述
当你将一个模块引入你的项目时,你如何在该模块中运行测试用例?
即我完成了:
go get my.repo.com/repo/mymodule
然后尝试测试其中的某些内容:
服务器>去测试src / my.repo.com / repo / mymodule / my_test.go
#命令行参数
src / my.repo.com / repo / mymodule / article_test.go:4:2 :找不到包mymodule中的任何一个:
我们应该单独检查我们的模块,那样测试?我不知道该怎么做。看起来,当我
去运行
它知道如何找到我提取的模块,但是当我去测试
时,它在路径中找不到它。解决方案
go test 期望包,而不是相对于 $ GOPATH
。
的文件夹因此,省略领先的
src /
和结尾的文件名称,它会工作:去测试我的。 repo.com/repo/mymodule
如果当前目录是要测试的包文件夹,甚至可以忽略它,例如
cd $ GOPATH / src / my.repo.com / repo / mymodule
go测试
参考 Command go / Test packages ,也运行
go help test
When you have pulled in a module, into your project, how do you run test cases within that module?
i.e. I have done:
go get my.repo.com/repo/mymodule
And then try to test something in it:
server> go test src/my.repo.com/repo/mymodule/my_test.go # command-line-arguments src/my.repo.com/repo/mymodule/article_test.go:4:2: cannot find package "mymodule" in any of:
Are we supposed to check out our modules separately and test that way? I can't quite work out what to do. It seems that when I
go run
it knows how to find the module I have fetched, but when Igo test
, it "can't find it" in the path.解决方案
go test
expects packages, not folders relative to$GOPATH
.So leave out the leading
src/
and the trailing file name, and it'll work:go test my.repo.com/repo/mymodule
If the current directory is the package folder you want to test, you can even omit it, e.g.
cd $GOPATH/src/my.repo.com/repo/mymodule go test
For reference see Command go / Test packages, also run
go help test
这篇关于当测试文件在模块中时,如何运行`go test`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文