go - 如何在 go 框架中为不同的测试执行测试顺序?

标签 go

如果我有不同的包并且每个包都有一个测试文件 (pkg_test.go),我有没有办法确保它们以特定的顺序运行?

假设 pkg1_test.go 首先执行,然后是其余部分。

我尝试使用 go channels,但它似乎挂起。

最佳答案

这并不明显,考虑到 go test ./... 会触发对所有包的测试...但并行运行:请参阅“Go: how to run tests for multiple packages?”。

go test -p 1 将按顺序运行测试,但不一定按您需要的顺序运行。

对按正确预期顺序列出的包调用 go test 的简单脚本会更容易执行。


6 年后更新:最佳做法是依赖测试顺序。
这么多issue 28592提倡将 -shuffle-shuffleseed 添加到 shuffle 测试中。

CL 310033提及:

This CL adds a new flag to the testing package and the go test command which randomizes the execution order for tests and benchmarks.

This can be useful for identifying unwanted dependencies between test or benchmark functions.

The flag is off by default.

  • If -shuffle is set to on then the system clock will be used as the seed value.
  • If -shuffle is set to an integer N, then N will be used as the seed value.

In both cases, the seed will be reported for failed runs so that they can reproduced later on.

commit cbb3f09 中选择了 Go 1.17(2021 年 8 月) .

在“Benchmarking with Go”中查看更多信息。

关于go - 如何在 go 框架中为不同的测试执行测试顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29268881/

相关文章:

datetime - 为什么 Go 中的时间在结构中打印不同?

go - 是否可以定义一个变量,其类型取决于 Golang 中的输入

go - Prometheus 添加自定义指标

loops - 为什么我会在 golang 中使用 `case <-time.After(time.Second * 1):` 而不是 `time.Sleep(time.Second * 1)`

ssh - 在没有 SSH 的情况下安装 Hugo Web 引擎

go - KafkaIO连接器/Apache Beam Transform“运行” SDK是否可用?

go - 如何匹配switch中的字符串

syntax - Go语言中的赋值运算符

golang所有goroutines都睡着了 - 死锁

go - 递归扩展结构定义?