当前位置: > > > > 尝试与 golang testify/suite 并行运行测试失败
尝试与 golang testify/suite 并行运行测试失败
来源:stackoverflow
2024-04-21 09:03:33
0浏览
收藏
欢迎各位小伙伴来到米云,相聚于此都是缘哈哈哈!今天我给大家带来《尝试与 golang testify/suite 并行运行测试失败》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
问题内容
我使用 testify/suite 包进行了多项测试,并且按如下方式并行执行它们
type integrationsuite struct {
suite.suite
}
func testintegrationsuite(t *testing.t) {
suite.run(t, &integrationsuite{})
}
func (is *integrationsuite) testsomething() {
is.t().log("\tintegration testing something")
for i := range mytesttable {
i := i
is.t().run("testing "+mytesttable[i].scenarioname, func(_ *testing.t) {
is.t().parallel()
...
func (is *integrationsuite) testsomethingelse() {
is.t().log("\tintegration testing something else")
for i := range myothertesttable {
i := i
is.t().run("testing "+myothertesttable[i].scenarioname, func(_ *testing.t) {
is.t().parallel()
...
})
但是这会引起恐慌
panic: testing: t.Parallel called multiple times [recovered]
panic: testing: t.Parallel called multiple times
如何利用特定包的并行性?
正确答案
您在 testing.t 的错误实例上调用 t.parallel()。
您正在使用以下方法启动多个子测试:
is.t().run("testing "+myothertesttable[i].scenarioname, func(_ *testing.t) {...}
但是,您不是将子测试标记为并行,而是将外部父测试标记为您启动的每个子测试的并行:
is.t().parallel() // marks outer test as parallel
相反,将子测试函数的 testing.t 参数绑定到变量并在子测试上调用 parallel:
is.T().Run("Testing "+myOtherTestTable[i].scenarioName, func(t *testing.T) {
t.Parallel() // this is the subtest's T instance
// test code
})
这将所有子测试标记为父测试中的并行测试。如果您还想将父测试标记为与包中的其他测试并行,请在 testsomethingelse 开头调用 is.t().parallel() once (不在子测试内)
有关子测试并行性的更多详细信息,请参阅:
请注意,testify 套件当前不是“线程安全”的,因此不支持并发。在某些情况下,即使测试应该失败,也会成功通过。
请在此处查找当前状态以及报告的问题:
理论要掌握,实操不能落!以上关于《尝试与 golang testify/suite 并行运行测试失败》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注米云公众号吧!
