英文:
Running all tests for a multi-binary project
问题
考虑一个具有以下结构的多二进制项目。
.
├── bin1
│ ├── config
│ │ ├── config.go
│ │ └── config_test.go
│ └── utils
│ ├── utils.go
│ └── utils_test.go
├── bin2
│ ├── config
│ │ ├── config.go
│ │ └── config_test.go
│ └── utils
│ ├── utils.go
│ └── utils_test.go
├── cmd
│ ├── bin1
│ │ └── bin1.go
│ ├── bin2
│ │ └── bin2.go
│ └── bin3
│ └── bin3.go
├── go.mod
├── go.sum
└── shared
├── db
│ ├── db.go
│ └── db_test.go
├── model
│ ├── modela.go
│ ├── modela_test.go
│ ├── modelb.go
│ └── modelb_test.go
└── utils
├── utils.go
└── utils_test.go
该项目有三个二进制文件 bin1、bin2 和 bin3。/shared 目录中的包(例如 shareddb、sharedmodel 和 sharedutils)与特定二进制文件的包共享(例如 /bin1 目录中的包 bin1config、bin1utils 和 /bin2 目录中的包 bin2config、bin2utils)。
我们如何运行:
-
该项目中的所有单元测试?
-
一个包中的所有测试(例如
shared/model中的测试)? -
每个测试分别运行?
我尝试了以下方法:
- 从项目根目录运行
go test结果显示/projectroot中没有 Go 文件。
英文:
Consider a multi binary project with the following structure.
.
├── bin1
│   ├── config
│   │   ├── config.go
│   │   └── config_test.go
│   └── utils
│   ├── utils.go
│   └── utils_test.go
├── bin2
│   ├── config
│   │   ├── config.go
│   │   └── config_test.go
│   └── utils
│   ├── utils.go
│   └── utils_test.go
├── cmd
│   ├── bin1
│   │   └── bin1.go
│   ├── bin2
│   │   └── bin2.go
│   └── bin3
│   └── bin3.go
├── go.mod
├── go.sum
└── shared
├── db
│   ├── db.go
│   └── db_test.go
├── model
│   ├── modela.go
│   ├── modela_test.go
│   ├── modelb.go
│   └── modelb_test.go
└── utils
├── utils.go
└── utils_test.go
This project has three binaries bin1, bin2 and bin3. Packages in the /shared directory (e.g. package shareddb, sharedmodel and sharedutils) are shared with binary specific packages (e.g. package bin1config, bin1utils in /bin1 directory and package bin2config, bin2utils in /bin2 directory).
How can we run
-
all the unit tests in this project altogether?
-
all the tests in a package (e.g. in
shared/model)? -
each tests separately?
I attempted the following.
- Running
go testfrom the project root resulted inno Go files in /projectroot.
答案1
得分: 2
# 运行所有测试
go test ./...
# 运行特定目录下(包括子目录)的所有测试
go test ./bin2/...
# 测试位于特定目录的包
go test ./shared/model
# 测试具有特定导入路径的包
go test projectroot/shared/model
# 测试当前工作目录中的包
go test
# 同上
go test .
# 测试父目录中的包
go test ..
# 运行被测试包中的特定测试
go test -run=X
# 运行被测试包中的特定子测试
go test -run=X/Y
有关go test命令的更多详细信息,请参阅测试包。
有关go test的[packages]参数的更多详细信息,请参阅包列表和模式。
有关测试标志的更多详细信息,请参阅测试标志。
英文:
# run all tests
go test ./...
# run all tests under a specific directory (including subdiretories)
go test ./bin2/...
# test package located in specific directory
go test ./shared/model
# test package that has specific import path
go test projectroot/shared/model
# test package in current working directory
go test
# ditto
go test .
# test package in parent directory
go test ..
# run a specific test within the package under test
go test -run=X
# run a specific sub-test within the package under test
go test -run=X/Y
For more details on the go test command, see Test packages.
For more details on the [packages] argument to go test, see Packge lists and patters.
For more details on the testing flags, see Testing flags.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论