如何在Go中随机打乱测试包的顺序?

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

How to shuffle testing order of packages in go?

问题

当我执行类似于以下命令时:

go test \
    github.com/mycompany/projectX/packageA \
    github.com/mycompany/projectX/packageB \
    github.com/mycompany/projectX/packageC

然后go会先测试packageA,然后是packageB,最后是packageC。是否有一个选项可以使包的测试顺序被打乱?例如,先测试packageC,然后是packageA,最后是packageB。有一个-shuffle on选项,但这个选项只会打乱包内的测试顺序。我想要打乱包的顺序。

英文:

When I execute a command similar to this:

go test \
    github.com/mycompany/projectX/packageA \
    github.com/mycompany/projectX/packageB \
    github.com/mycompany/projectX/packageC

Then go will test packageA, then packageB, and then packageC. Is there an option so that the order of package testing is shuffled? For instance, packageC is tested first before package A then package B. There is a -shuffle on option but this option only shuffles the tests within a package. I want to shuffle the order of the packages.

答案1

得分: 3

包总是按字母顺序进行测试,无法禁用。我不在那里,所以我必须猜测原因。

一般来说,单元测试不应依赖于执行顺序,因为在那时你测试的不仅仅是单元的功能。如果由于某种原因,你的单元测试依赖于执行顺序,并且这个顺序总是固定的,你仍然会得到一致的结果。但是,如果这个顺序在某种程度上是随机的或者依赖于命名或者指定顺序,那么这意味着一个测试在你的机器上可能通过,但在 CI(持续集成)上失败,反之亦然。

这种错误非常令人沮丧,因为测试顺序影响其结果可能并不明显。

话虽如此,如果你愿意,你仍然可以使用T.Run方法以随机顺序进行测试。但这只适用于同一个包内部,而不适用于跨包。

英文:

Packages are always tested in alphabetical order, no way to disable it. I wasn't there so I have to guess at the reason.

In general unit tests should not be dependent on the execution order since at that point you are testing more than just the functionality of your unit. If for some reason your unit tests are dependent on execution order, and this order is always fixed, you will still get consistent results. But if this order was somehow random or dependent on naming or order in which you specify them, than it means that a test may pass on your machine but fail in the CI or visa versa.

Such bugs are extremely frustrating since it might not be obvious that the order of tests influences its result.

Having said all that, if you wish you can still randomize testing using the T.Run method in a random order. But this only works within the same package not across packages.

huangapple
  • 本文由 发表于 2021年11月3日 08:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/69818513.html
匿名

发表评论

匿名网友

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

确定