首页 > 解决方案 > 银杏清理失败

问题描述

我正在用 Ginkgo 编写测试规范。

我的测试具有以下结构:

It("Setup X, Y, Z resources and check conditions" func() {
    // setup resources. 
    // assert certain conditions using
    //cleanup resources
}) 

我的问题是,断言失败时如何执行清理。如果我为此目的使用 afterEach 块,那么它会对所有测试规范执行相同的清理,这会显示一堆清理消息失败。

用银杏清理失败的推荐方法是什么。

标签: goginkgogomega

解决方案


您可以将此测试保存在单独的上下文中。然后afterEach将仅适用于该上下文下的所有It :

Context("Setup X, Y, Z resources and check conditions", func() {
    BeforeEach(func() {
        // do ...
    })

    AfterEach(func() {
        // do clean up
    })

    It("should pass the condition", func() {
        // do ...
    })
})

推荐阅读