英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论