go build可以正常工作,但go test无法正常工作。

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

go build works but go test does not

问题

以下是我的项目结构:

/user/home/go/src/github.com/my_go_proj
                                       /main.go
                                       /mypackage
                                                 /service.go
                                                 /service_test.go

GOPATH指向/user/home/go
cd /user/home/go/src/github.com/my_go_proj
go build ---> 这样可以创建my_go_proj可执行文件。
go test
? github.com/my_go_proj [没有测试文件]
go test github.com/my_go_proj/mypackage
go build gopkg.in/tomb.v2: 在中没有可构建的Go源文件
FAIL github.com/my_go_proj/mypackage [构建失败]

go test ./...
? github.com/my_go_proj [没有测试文件]
go build gopkg.in/tomb.v2: 在中没有可构建的Go源文件
FAIL github.com/my_go_proj/mypackage [构建失败]

如何运行go test以运行mypackage中的service_test.go测试?

更新: 更新了go test ./...的行为

英文:

Below is my project structure:
<pre>
/user/home/go/src/github.com/my_go_proj
/main.go
/mypackage
/service.go
/service_test.go

GOPATH points to /user/home/go
cd /user/home/go/src/github.com/my_go_proj
go build ---> This works and creates the my_go_proj executable.
go test
? github.com/my_go_proj [no test files]
go test github.com/my_go_proj/mypackage
go build gopkg.in/tomb.v2: no buildable Go source files in
FAIL github.com/my_go_proj/mypackage [build failed]

go test ./...
? github.com/my_go_proj [no test files]
go build gopkg.in/tomb.v2: no buildable Go source files in
FAIL github.com/my_go_proj/mypackage [build failed]

</pre>

How do I run go test to run the service_test.go test inside mypackage?

Update: updated the behaviour for go test ./...

答案1

得分: 1

要测试所有子目录,请使用以下命令:

$ go test ./...

根据Command go文档中的相对导入路径部分:

也可以使用相对模式,例如 go test ./... 来测试所有子目录。有关模式语法的详细信息,请参阅 'go help packages'。

英文:

To test all subdirectories use:

$ go test ./...

From the Relative import paths section of the Command go documentation:

> Relative patterns are also allowed, like go test ./... to test all subdirectories. See 'go help packages' for details on the pattern syntax.

答案2

得分: 0

问题出在目录结构上。我的项目源代码应该在$GOPATH中,但实际上它在$GOPATH/src中。

错误的:/user/home/go/src/github.com/my_go_proj <br>
正确的:/user/home/go/github.com/my_go_proj

英文:

The problem was with the directory structure. My project source should be in $GOPATH. But it was inside $GOPATH/src.

Incorrect /user/home/go/src/github.com/my_go_proj <br>
Correct /user/home/go/github.com/my_go_proj

huangapple
  • 本文由 发表于 2015年12月5日 00:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/34093333.html
匿名

发表评论

匿名网友

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

确定