真正的单元测试是否总是可以并行运行?

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

Can true unit tests always be run in parallel?

问题

背景:

我正在编写很多Go代码,使用go test工具和提供的"testing"包进行测试。我进行的大部分测试都是单元测试,遵循TDD原则。这些被测试的"单元"不允许依赖于有状态的外部因素,比如持久化存储、网络跳跃等,而是通常接收虚拟的内存实现这些外部因素的"构造器/生成器"函数(是的,我知道它们不是传统意义上的构造函数)。

问题:

长期以来,go test工具总是以相同的确定性顺序运行测试函数,这一点一直让我感到困扰。在某些情况下,这可能导致竞态条件在代码中隐藏。找到这些错误的一种方法是设置-race标志。另一种方法可能是始终并行运行单元测试...

问题:

是否存在任何情况下,隔离的单元测试不能或不应该始终使用-parallel标志并行运行?

英文:

Background:

I'm writing a lot of go code, using the go test tool and the provided "testing" package for, well, testing. Most of the testing that I do is unit testing, within the TDD discipline. These "units" under test are never permitted to depend on stateful externalities like persistent storage, a network hop, etc., but receive fake, in-memory implementations of those externalities usually in "constructor/builder" functions (yes, I know they aren't constructors in the traditional sense).

The Problem:

It has long bothered me that the go test tool always runs test functions in the same deterministic order. This has, in a few cases, allowed race conditions to hide in the code. One way to find these bugs is by setting the -race flag. Another might be to always run unit tests in parallel...

The Question:

Is there ever a situation in which isolated unit tests could not or should not always be run in parallel (using the -parallel flag)?

答案1

得分: 1

parallel标志实际上是一种测试测试本身的方式(除了加快测试速度)。当以非确定性顺序运行时,发现测试随机中断,这既可能表示测试有问题,也可能表示代码有问题。

Ruby的测试运行器(包括Rspec和Minitest)做了一些有趣的事情-它们在每次运行时随机排列测试顺序,但会打印出用于该运行的整数种子。这样可以重现错误,同时确保没有测试依赖于隐式排序。我不知道有任何Go测试运行器这样做,但这将非常有用。

英文:

The parallel flag is actually a way to test the tests themselves (besides speeding them up). Finding a test breaks randomly when run in non deterministic order indicates a bad test as often as it does bad code.

Ruby's test runners do something interesting (both Rspec and Minitest) - they randomize the test order on each run, but print an integer seed that was used for the run. This allows the error to be reproduced while still making sure that no tests depend on implicit ordering. I don't know of any Go test runner doing this, but that would be quite useful.

huangapple
  • 本文由 发表于 2015年9月8日 23:44:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/32462023.html
匿名

发表评论

匿名网友

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

确定