简体   繁体   English

如何在多模块 Go 项目中运行测试

[英]How to run tests in a multimodule Go project

I'm learning Go with the specialization Programming with Google Go .我正在学习 Go 专业Programming with Google Go It has several courses, each of which has several modules.它有几门课程,每门课程都有几个模块。

My project looks like the following (made with https://ascii-tree-generator.com/ ):我的项目如下所示(使用https://ascii-tree-generator.com/ 制作):

google-golang/
├─ .github/
│  ├─ workflows/
│  │  ├─ ci.yml
├─ golang-getting-started/
│  ├─ module1/
│  │  ├─ main.go
│  │  ├─ main_test.go
│  ├─ module2/
│  │  ├─ trunc/
│  │  │  ├─ main.go
│  │  │  ├─ main_test.go
├─ .gitignore
├─ README.md

I'd like to run all the tests in the *_test.go files for every commit , and it's not clear to me how to do that from the root directory ( google-golang ).我想为每次提交运行*_test.go文件中的所有测试,但我不清楚如何从根目录 ( google-golang ) 执行此操作。 I don't see a need for one module to import another as the exercises can be done independently.我认为不需要一个模块导入另一个模块,因为练习可以独立完成。 This question has two answers, one suggests using submodules, another recommends using Go workspace, but neither provide specific instructions that someone new like me can learn from.这个问题有两个答案,一个建议使用子模块,一个建议使用 Go 工作空间,但都没有提供具体说明,像我这样的新手可以学习。

I'm not asking for help on GitHub Actions, I know how to write those.我不是在寻求有关 GitHub 操作的帮助,我知道如何编写这些操作。 I'm looking for one or more commands that'll find and run the tests.我正在寻找一个或多个可以找到并运行测试的命令。

You seem confused about what a module is.您似乎对什么是模块感到困惑。 The rule of thumb is, one go.mod file equals one module.经验法则是,一个go.mod文件等于一个模块。 Go Wiki, gomod : Go 维基,gomod

A module is defined by a tree of Go source files with a go.mod file in the tree's root directory.模块由 Go 源文件树定义,在树的根目录中具有 go.mod 文件。

Based on your directory tree shown in your question, there is no go.mod file in sight, hence nothing there is a Go module.根据问题中显示的目录树,看不到go.mod文件,因此没有 Go 模块。 As a matter of fact, if you attempt running a module-aware command from google-golang or golang-getting-started you'll have:事实上,如果您尝试从google-golanggolang-getting-started运行模块感知命令,您将拥有:

go: go.mod file not found in current directory or any parent directory; go:在当前目录或任何父目录中找不到 go.mod 文件; see 'go help modules'见“去帮助模块”

If you want to run all tests from the root of a multi-module repo, as the title of your question says, with Go 1.17 you can:如果您想从多模块存储库的根目录运行所有测试,如您的问题标题所述,使用 Go 1.17 您可以:

  1. init the sub-modules:初始化子模块:
$ cd google-golang/golang-getting-started/module1
$ go mod init example.com/module1
$ cd ../module2
$ go mod init example.com/module2
  1. use the trick suggested by Cerise Limón from the root dir of the multi-module project使用Cerise Limón 从多模块项目的根目录中建议的技巧
$ cd google-golang
$ find . -name go.mod -execdir go test ./... \;

If you don't actually care about keeping the sub-repos as separate modules, you can init a module in the root repo and run all tests from there:如果您实际上并不关心将子存储库保留为单独的模块,则可以在根存储库中初始化一个模块并从那里运行所有测试:

$ cd google-golang # or google-golang/golang-getting-started/
$ go mod init example.com/mainmod
$ go test ./...

...however, even this hack works right now, it doesn't make a lot of sense, because your repos named moduleN have main.go files, whose packages are supposedly named main , in each of them. ...但是,即使这个 hack 现在有效,它也没有多大意义,因为您的名为moduleN的 repos 有main.go文件,其包被称为main ,在每个文件中。 So they are indeed organized as separate sub-modules.因此它们确实被组织为单独的子模块。

try: go test./...尝试:go 测试。/...

./... is recursively find all files ./... 是递归查找所有文件

If you want to do more, you can learn go:generate https://go.dev/blog/generate如果你想做更多,你可以学习 go:generate https://go.dev/blog/generate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM