Testify似乎同时运行测试套件吗?

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

Testify is seemingly running test suites concurrrently?

问题

基本上,我在特定的包中创建了一个新的测试文件,其中包含一些基本的测试结构 - 没有实际的测试...只是一个空的结构类型,它嵌入了suite.Suite,并且有一个函数,该函数接受一个*testing.T对象,并在该结构上调用suite.Run()。这立即导致我们所有其他的测试开始不确定地失败。

这些失败的性质与在单个Postgres数据库中插入和删除时与数据库唯一键完整性冲突有关。这让我相信测试是并发运行的,而没有在测试之间正确调用我们的设置方法来准备环境。

不用说,一旦我将这个测试文件移动到另一个包中,一切都神奇地正常工作了!

有其他人遇到过这个问题,并且可能能提供一些见解吗?

Testify似乎同时运行测试套件吗?

英文:

Basically I created a new test file in a particular package with some bare bones test structure - no actual tests...just an empty struct type that embeds suite.Suite, and a function that takes in a *testing.T object and calls suite.Run() on said struct. This immediately caused all our other tests to start failing indeterministically.

The nature of the failures were associated with database unique key integrity violations on inserts and deletes into a single Postgres DB. This is leading me to believe that the tests were being run concurrently without calling our setup methods to prepare the environment properly between tests.

Needless to say, the moment I move this test file to another package, everything magically works!

Has anyone else run into this problem before and can possibly provide some insights?

Testify似乎同时运行测试套件吗?

答案1

得分: 4

原来,这个问题是与go test的工作方式有关,与testify无关。我们的测试是在**./...上运行的。这导致底层的go test工具并行运行每个包中的测试,正如justinas指出的那样。在StackOverflow上进一步查找(这里这里)并阅读testify关于这个问题的活动问题后,似乎最好的即时解决方案是使用-p=1**标志来限制并行运行的包数量。

然而,仍然无法解释为什么在添加这些新的包之前测试始终通过。一个猜想是,也许包/测试文件被排序并以这样的方式运行,以前添加新的包/文件时并发不是一个问题。

英文:

As it turns out, this is a problem rooted in how go test works, and has nothing to do with testify. Our tests were being ran on ./... This causes the underlining go test tool to run tests in each package in parallel, as justinas pointed out. After digging around more on StackOverflow (here and here) and reading through testify's active issue on this problem, it seems that the best immediate solution is to use the -p=1 flag to limit the number of packages to be run in parallel.

However, it is still unexplained why the tests consistently passed prior to adding these new packages. A hunch is perhaps the packages/test files were sorted and ran in such a manner that concurrency wasn't an issue prior to adding the new packages/files.

答案2

得分: 3

我发现,通过使用"go test"命令,可以按顺序运行单个包的测试用例(除非调用了t.Parallel()函数),但如果你提供多个包(例如"go test ./foo ./bar ./baz"),每个包的测试将与其他包并行运行。这对于数据库测试也会带来类似的困扰。

英文:

What I've found from my use, is that "go test" runs a single package's test cases sequentially (unless t.Parallel() is called), but if you supply multiple packages (go test ./foo ./bar ./baz), each package's tests are run parallel to other packages. Definitely caused similar headaches with database testing for me.

huangapple
  • 本文由 发表于 2014年10月30日 06:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/26641454.html
匿名

发表评论

匿名网友

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

确定