如果我有不同的包并且每个包都有一个测试文件 (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 toon
then the system clock will be used as the seed value.- If
-shuffle
is set to an integerN
, thenN
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/