运行多二进制项目的所有测试

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

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

该项目有三个二进制文件 bin1bin2bin3/shared 目录中的包(例如 shareddbsharedmodelsharedutils)与特定二进制文件的包共享(例如 /bin1 目录中的包 bin1configbin1utils/bin2 目录中的包 bin2configbin2utils)。

我们如何运行:

  • 该项目中的所有单元测试?

  • 一个包中的所有测试(例如 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 test from the project root resulted in no 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.

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

发表评论

匿名网友

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

确定