在并行子测试仍在运行时执行 TearDownTest
作者: swithek创建于 2020年4月27日更新于 2026年7月23日
标签bug
考虑以下示例: go type Suite struct { suite.Suite }
func (s *Suite) TearDownSuite() { s.T().Log(">>>>>> suite tear down") }
func (s *Suite) TearDownTest() { s.T().Log(">>>>>> single test tear down") }
func (s *Suite) TestOne() { for _, v := range []string{"sub1", "sub2", "sub3"} { s.Run(v, func() { s.T().Parallel() }) } }
func (s *Suite) TestTwo() { for _, v := range []string{"sub1", "sub2", "sub3"} { s.Run(v, func() { s.T().Parallel() }) } }
func TestLogic(t *testing.T) { suite.Run(t, &Suite{}) }
执行后,您将得到类似以下的输出:
=== RUN TestLogic
=== RUN TestLogic/TestOne
=== RUN TestLogic/TestOne/sub1
=== PAUSE TestLogic/TestOne/sub1
=== RUN TestLogic/TestOne/sub2
=== PAUSE TestLogic/TestOne/sub2
=== RUN TestLogic/TestOne/sub3
=== PAUSE TestLogic/TestOne/sub3
TestLogic/TestOne: prog.go:18: >>>>>> single test tear down
=== CONT TestLogic/TestOne/sub1
=== CONT TestLogic/TestOne/sub3
=== CONT TestLogic/TestOne/sub2 >>> test finished here
=== RUN TestLogic/TestTwo
=== RUN TestLogic/TestTwo/sub1
=== PAUSE TestLogic/TestTwo/sub1
=== RUN TestLogic/TestTwo/sub2
=== PAUSE TestLogic/TestTwo/sub2
=== RUN TestLogic/TestTwo/sub3
=== PAUSE TestLogic/TestTwo/sub3
TestLogic/TestTwo: prog.go:18: >>>>>> single test tear down
=== CONT TestLogic/TestTwo/sub1
=== CONT TestLogic/TestTwo/sub3
=== CONT TestLogic/TestTwo/sub2 >>> test finished here
TestLogic: prog.go:14: >>>>>> suite tear down实时示例: https://play.golang.com/p/fPY3DZFNNok 很明显,TearDownTest 在所有并行子测试完成之前就被执行了。我看到类似的问题在 #799 和 #466 中已被解决,但并未完全解决。使用的版本是 1.5.1
内容来源: stretchr/testify