ginkgo icon indicating copy to clipboard operation
ginkgo copied to clipboard

QUESTION: How to trigger FailHandler when Expect fails in BeforeAll section

Open konopka90 opened this issue 4 months ago • 4 comments

Hi, I have an problem with setting up my test suite and maybe you got some ideas how to improve my approach. Inside BeforeSuite I set up RegisterFailHandler and it is working correctly when Expect fails in BeforeEach section.

However, when Expect fails in BeforeAll section then fail handler is not launched. How can I make sure that my fail handler will launch in this case? I figured out one solution by calling defer GinkgoRecover() on first line in every BeforeAll section but it does not looks good for me and I don't have any proof that it should be done in this way.

konopka90 avatar Aug 28 '25 07:08 konopka90

hey there @konopka90 - RegisterFailHandler is meant to be called in the TestX go test function that calls ginkgo.RunSpecs and it primarily exists to connect Ginkgo with Gomega. It’s a Gomega function that connects the Gomega singleton with Ginkgo’s Fail.

Which leads me to ask: what is the problem you are actually trying to solve?

onsi avatar Aug 28 '25 13:08 onsi

@onsi What I'm trying to achieve is to collect some logs from a system when any of my tests fails. Unfortunately, I got some pre-test logic that should be executed only once per suite in BeforeAll step. I expected that FailHandler will also run when something fails in BeforeAll step. That's why I'm asking about that.

konopka90 avatar Nov 05 '25 09:11 konopka90

Can you share your code with me?

If you have setup code that should run once before the suite it should go in BeforeSuite - not BeforeAll.

And if you have logs that need to be emitted on failure you can send them to GinkgoWriter and it will automatically include them in the output when a failure occurs.

onsi avatar Nov 05 '25 13:11 onsi

Alternatively, if what you want to do is query an external system but only after a failure you can add a ReportAfterEach.

https://onsi.github.io/ginkgo/#reporting-nodes---reportaftereach-and-reportbeforeeach

onsi avatar Nov 05 '25 14:11 onsi