英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论