gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:48:15 25 4
gpt4 key购买 nike

如果我有不同的包并且每个包都有一个测试文件 (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 commandwhich randomizes the execution order for tests and benchmarks.

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

The flag is off by default.

  • If -shuffle is set to on then the systemclock 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com