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

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

How to run tests in a multimodule Go project

问题

我正在学习使用谷歌Go专项课程《与谷歌Go编程》。它包含了几门课程,每门课程都有几个模块。

我的项目结构如下(使用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

我想要在每次提交时运行所有*_test.go文件中的测试,但我不清楚如何从根目录(google-golang)执行这个操作。我认为不需要一个模块导入另一个模块,因为这些练习可以独立完成。这个问题有两个答案,一个建议使用子模块,另一个建议使用Go工作区,但都没有提供具体的指导让像我这样的新手学习。

我并不是在寻求有关GitHub Actions的帮助,我知道如何编写那些。我正在寻找一个或多个命令来查找并运行测试。

英文:

I'm learning Go with the specialization 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/):

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). 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.

I'm not asking for help on GitHub Actions, I know how to write those. I'm looking for one or more commands that'll find and run the tests.

答案1

得分: 5

你似乎对模块的概念有些困惑。一般来说,一个 go.mod 文件对应一个模块。根据你在问题中展示的目录结构,看不到任何 go.mod 文件,因此那里没有任何 Go 模块。实际上,如果你尝试从 google-golanggolang-getting-started 运行一个支持模块的命令,你会得到以下提示:

go: go.mod 文件在当前目录或任何父目录中都找不到;请参阅 'go help modules'

如果你想在一个多模块仓库的根目录下运行所有测试,可以使用 Go 1.17 的以下方法:

  1. 初始化子模块:
$ cd google-golang/golang-getting-started/module1
$ go mod init example.com/module1
$ cd ../module2
$ go mod init example.com/module2
  1. 使用 Cerise Limón 提出的技巧,在多模块项目的根目录中运行以下命令:
$ cd google-golang
$ find . -name go.mod -execdir go test ./... \;

如果你实际上并不关心将子仓库保持为独立的模块,你可以在根仓库中初始化一个模块,并从那里运行所有测试:

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

然而,即使这个方法目前可以工作,它也没有太多意义,因为你命名为 moduleN 的仓库中都有一个 main.go 文件,其中的包名应该是 main。因此,它们确实是作为独立的子模块组织的。

英文:

You seem confused about what a module is. The rule of thumb is, one go.mod file equals one module. Go Wiki, gomod:

> A module is defined by a tree of Go source files with a go.mod file in the tree's root directory.

Based on your directory tree shown in your question, there is no go.mod file in sight, hence nothing there is a Go module. As a matter of fact, if you attempt running a module-aware command from google-golang or golang-getting-started you'll have:

> go: go.mod file not found in current directory or any parent directory; 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:

  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
$ cd google-golang
$ find . -name go.mod -execdir go test ./... \;

<hr>

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. So they are indeed organized as separate sub-modules.

huangapple
  • 本文由 发表于 2021年12月6日 16:41:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/70242784.html
匿名

发表评论

匿名网友

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

确定