如何设置 Ginkgo 测试套件?

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

How do I set up a Ginkgo test suite?

问题

我继承了一个Go项目,其中包含许多共用文件、一种类似库的东西、两个可执行文件,理论上还有一个测试套件。测试套件是事后编写的。但是我不喜欢目前找到的设置方式,它相当不可取。

我正在使用Ginkgo,并且这是我的起始目录结构:

  • component1/component1.go
  • component2/component2.go
  • cmd1/cmd1.go
  • cmd2/cmd2.go
  • project_suite_test.go
  • component1_test.go

每个cmd?.go文件将被编译成一个单独的可执行文件。

我想要的是一个多文件的测试套件,通常每个组件一个文件。我应该把这些文件放在哪里,以便go test能够找到并运行它们,而不是将它们留在项目的根目录中?

英文:

I have inherited a Go project that consists of a lot of common files, a library of sorts, two executables, and theoretically a test suite. The test suite is being written after the fact. But I dislike the only way I've found of setting up is rather unpalatable

I'm using Ginkgo, and this is my starting directory structure

  • component1/component1.go
  • component2/component2.go
  • cmd1/cmd1.go
  • cmd2/cmd2.go
  • project_suite_test.go
  • component1_test.go

Each cmd?.go file will be compiled into a separate executable.

What I would like is a multi-file test suite, usually one file per component. Where do I put the files so that go test will find and run all of them, without leaving them here in the root of the project?

答案1

得分: 2

ginkgo initginkgo bootstrap 将设置你的测试环境。ginkgo -r 将递归地运行所有的测试。

英文:

ginkgo init and ginkgo bootstrap will set up your tests. ginkgo -r will run all your tests recursively.

答案2

得分: 1

原因:
只有在通过ginkgo实际引导项目后,ginkgo命令才能正常工作。

选项:
要使用该命令,您需要在终端中转到测试目录并运行以下命令:

ginkgo init:初始化项目。

ginkgo bootstrap:这将生成一个带有测试套件配置的新文件。

ginkgoginkgo test:现在可以根据生成的新文件运行测试,因为它正在尝试搜索该文件。

或者:
如果您希望将测试保存在子文件夹(例如test)中,则运行以下命令:

go test ./...

将尝试在每个文件夹中运行测试,即使其中某些文件夹不包含任何测试,也会在后续报告中对非测试文件夹显示问号。

相反,如果运行以下命令:

go test ./.../test

将仅针对测试文件夹,从而获得仅关注测试文件夹的干净报告。

您还可以使用'go run $(ls *.go)'来运行给定文件夹中的所有文件。请注意,括号内有正则表达式。

如果您想在不同路径中运行测试,请根据所需目录在正则表达式中更新路径。

英文:

Reason:
Ginkgo command will only work if you have actually bootstrap your project via ginkgo.

Options:
To use that you have to go to your test dir in terminal and run

ginkgo init : To Initialise project:

ginkgo bootstrap : This will generate new file with test suite config

ginkgo or ginkgo test : this will now be able to run tests based on your new generated file because that's what it is trying to search.

Alternatively:

If you like to keep your tests in a sub-folder, say test, then running

go test ./...

will attempt to run tests in every folder, even those that do not contain any test, thus having a ? in the subsequent report for non-test folders.

Running

go test ./.../test

instead will target only your test folders, thus having a clean report focused on your tests folders only.

you can alternatively use 'go run $(ls *.go)' to run all the files in a given folder.
Notice you have regular expression within () braces.

In-case you want to run test in different path update path as per your desired dir in the regular expression

答案3

得分: 0

你可以在根目录下使用go test ./...命令,它会进入子文件夹并执行测试:

component1/component1.go
component1/component1_test.go
component2/component2.go
component2/component2_test.go
cmd1/cmd1.go
cmd1/cmd1_test.go
cmd2/cmd2.go
cmd2/cmd2_test.go
英文:

You can use go test ./... in the root and it will go into child folders and execute the tests:

component1/component1.go
component1/component1_test.go
component2/component2.go
component2/component2_test.go
cmd1/cmd1.go
cmd1/cmd1_test.go
cmd2/cmd2.go
cmd2/cmd2_test.go

huangapple
  • 本文由 发表于 2014年9月17日 06:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/25879473.html
匿名

发表评论

匿名网友

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

确定