当测试文件在模块中时,如何运行`go test`? [英] How do you run `go test` when test files are within a module?

查看:320
本文介绍了当测试文件在模块中时,如何运行`go test`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你将一个模块引入你的项目时,你如何在该模块中运行测试用例?

即我完成了:

  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 I go 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆