diff --git a/test_main.go b/test_main.go index 39f87f5f..a208a6aa 100644 --- a/test_main.go +++ b/test_main.go @@ -76,12 +76,20 @@ func TestMain(m *testing.M, namespace string, customOpts ...opt) { fmt.Printf("Error: %s", err) os.Exit(1) } - exitCode := m.Run() - if opts.cleanup != nil { - opts.cleanup(testPackage.Config) + + // Run the cleanup functions even on panic. + // Note that deferred functions aren't run on `os.Exit`, so we need to put the `defer` calls + // inside a new `func()`. + runAndCleanup := func() int { + defer testPackage.Cleanup() + if opts.cleanup != nil { + defer opts.cleanup(testPackage.Config) + } + + return m.Run() } - testPackage.Cleanup() - os.Exit(exitCode) + + os.Exit(runAndCleanup()) } // Deploy will deploy the given blueprint or terminate the test.