英文:
go test ./... missing tests in subfolders
问题
在根目录下运行go test -v ./...
会错过项目文件夹中的一些测试。
我的文件结构如下:
.
├── cmd
│ └── Myapp
│ ├── config_test.go
│ └── main.go
├── config.yml
├── Myapp
├── go.mod
├── go.sum
├── images
│ └── Dockerfile.dev
├── kubernetes
│ └── deployment.yml
├── LICENSE.md
├── pkg
│ └── AFolder
│ ├── Bookmark.go
│ ├── Bookmark_test.go
│ ├── go.mod
│ ├── go.sum
│ ├── end.go
│ ├── end.go
│ ├── Handler.go
│ ├── Handler_test.go
│ ├── UpdateHandler.go
│ └── UpdateHandler_test.go
├── README.md
├── renovate.json
└── skaffold.yaml
位置是../go/src/Myapp
,go路径是../go
我可以通过在根目录下使用go build ./...
进行构建,并创建一个二进制文件。
但是运行go test -v ./...
只会运行config_test.go
中的测试,并错过了pkg
子文件夹中的测试。
文件结构可能是问题的首要怀疑。但我不确定如何解决它。任何建议将不胜感激。
英文:
Running go test -v ./...
from the root directory misses some tests in the folders of my project.
My file structure is as follows
.
├── cmd
│ └── Myapp
│ ├── config_test.go
│ └── main.go
├── config.yml
├── Myapp
├── go.mod
├── go.sum
├── images
│ └── Dockerfile.dev
├── kubernetes
│ └── deployment.yml
├── LICENSE.md
├── pkg
│ └── AFolder
│ ├── Bookmark.go
│ ├── Bookmark_test.go
│ ├── go.mod
│ ├── go.sum
│ ├── end.go
│ ├── end.go
│ ├── Handler.go
│ ├── Handler_test.go
│ ├── UpdateHandler.go
│ └── UpdateHandler_test.go
├── README.md
├── renovate.json
└── skaffold.yaml
The location is ../go/src/Myapp
and the go path is ../go
I can build from the root fine with go build ./...
and creates a binary.
But running go test -v ./...
will only run the tests in the config_test.go
and miss out on the tests in the pkg subfolders.
A problem with the file structure would be my first suspicion. But I am not sure how to go about fixing it. Any advice would be appreciated.
答案1
得分: 2
问题是由AFolder文件夹中的go.mod文件引起的。
./...模式匹配当前模块中的所有包。AFolder不是当前模块,因为它有自己的go.mod文件。换句话说,单元测试会排除所有带有go.mod文件的子文件夹。
go test all
将测试所有依赖项。然而,这是耗时且可能是不必要的。
在我的项目中,我只在根文件夹中创建go.mod文件。例如:
1)在根文件夹中运行go mod init github.com/cyrilzh/myproject
2)创建子文件夹"sub"并创建名为sub的包的代码
3)在项目中,像这样引用sub包:
import "github.com/cyrilzh/myproject/sub"
更多信息,请参考
英文:
The problem is caused by go.mod file in AFolder folder.
The ./... pattern matches all the packages within the current module. AFolder is not the current module as it has its own go.mod file. In other words, unit test exclude all the subfolders with a go.mod file.
go test all
will test all the dependencies. However, this is time consuming and might be unnecessary .
In my project, I just create go.mod file in root folder. For example:
-
In root folder run
go mod init github.com/cyrilzh/myproject
-
Create sub folder "sub" and create code with package named sub
-
In the project, reference the sub package like this:
import "github.com/cyrilzh/myproject/sub"
For more information, please refer to
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论