运行 `go test ./…` 命令时,出现了子文件夹中缺少测试的情况。

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

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"

更多信息,请参考

golang wiki: 如何定义一个模块

英文:

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:

  1. In root folder run go mod init github.com/cyrilzh/myproject

  2. Create sub folder "sub" and create code with package named sub

  3. In the project, reference the sub package like this:

    import "github.com/cyrilzh/myproject/sub"

For more information, please refer to

golang wiki: how to define a module

huangapple
  • 本文由 发表于 2022年4月4日 10:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/71731406.html
匿名

发表评论

匿名网友

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

确定