ginkgo cleanup on failure

1.1k Views Asked by At

I am writing my test specs in Ginkgo.

My tests have the following structure:

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

My question is, how do I perform cleanup when assertion fails. If I use afterEach block for this purpose, then it executes same cleanup on all test specs, which shows with a bunch of failures to cleanup messages.

what's the recommended way to cleanup on failure with ginkgo.

1

There are 1 best solutions below

0
On

You can keep this test in seperate context. Then afterEach will only apply to all the It under that context:

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 ...
    })
})