如何在Go框架中强制执行不同测试的测试顺序?

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

How to enforce testing order for different tests in a go framework?

问题

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

比如先执行pkg1_test.go,然后再执行其他的。

我尝试使用Go通道,但似乎会卡住。

英文:

If I have different packages and each have a test file (pkg_test.go) is there a way for me to make sure that they run in a particular order ?

Say pkg1_test.go gets executed first and then the rest.

I tried using go channels but it seems to hang.

答案1

得分: 1

考虑到go test ./...会触发所有包的测试,但是会并行运行,这一点并不明显,可以参考"Go:如何运行多个包的测试?"。

go test -p 1会按顺序依次运行测试,但不一定是你所需的顺序。

一个简单的脚本可以按正确的顺序调用go test来测试列出的包,这样更容易实现。

更新6年后:最佳实践是不要依赖测试的顺序。
事实上,issue 28592 提倡添加-shuffle-shuffleseed来对测试进行随机排序。

CL 310033 提到:

> 这个 CL 向 testing 包和 go test 命令添加了一个新的标志,用于随机执行测试和基准测试。
>
> 这对于识别测试或基准测试函数之间的不必要的依赖关系非常有用。
>
> 默认情况下,该标志是关闭的。
>
> - 如果将 -shuffle 设置为 on,则系统时钟将用作种子值。
> - 如果将 -shuffle 设置为整数 N,则 N 将用作种子值。
>
> 在这两种情况下,种子将在运行失败时报告,以便以后可以重现。

在 Go 1.17(2021 年 8 月)中采用了 commit cbb3f09

请参阅"使用 Go 进行基准测试"了解更多信息。

英文:

It isn't obvious, considering a go test ./... triggers test on all packages... but runs in parallel: see "Go: how to run tests for multiple packages?".

go test -p 1 would run the tests sequentially, but not necessarily in the order you would need.

A simple script calling go test on the packages listed in the right expected order would be easier to do.


Update 6 years later: the best practice is to not rely on test order.
So much so issue 28592 advocates for adding -shuffle and -shuffleseed to shuffle tests.

CL 310033 mentions:

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

Picked up for Go 1.17 (Aug. 2021) in commit cbb3f09.

See more at "Benchmarking with Go".

答案2

得分: 0

我找到了一个解决方法来绕过这个问题。
我将我的测试文件命名如下:

A_{test_file1}_test.go
B_{test_file2}_test.go
C_{test_file3}_test.go

A、B、C会确保它们按顺序运行。

英文:

I found a hack to get around this.
I named my test files as follow:

A_{test_file1}_test.go
B_{test_file2}_test.go
C_{test_file3}_test.go

The A B C will ensure they are run in order.

huangapple
  • 本文由 发表于 2015年3月26日 08:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/29268881.html
匿名

发表评论

匿名网友

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

确定