ginkgo icon indicating copy to clipboard operation
ginkgo copied to clipboard

How do I get the text of describe during runtime?

Open mkoppal-px opened this issue 2 years ago • 3 comments

For example, I have a code like this

var _ = Describe("Foo Bar test", func() {
    JustBeforeEach(func() {
        fmt.Println(<name of the text in describe>) // Foo Bar test
    }
}

How do I extract Foo Bar test whenever I need it during runtime, so that I can use it in the logs to print it.

mkoppal-px avatar Feb 17 '23 14:02 mkoppal-px

Hey you can do this:

var _ = Describe("Foo Bar test", func() {
    JustBeforeEach(func() {
        report := CurrentSpecReport()
        fmt.Println(report.ContainerHierarchyTexts[0]) // Foo Bar test
    }
}

More details here:

https://pkg.go.dev/github.com/onsi/ginkgo/[email protected]/types#SpecReport

note that report.FullText() will give you the fully concatenated text (i.e. all containers + the It text). and ContainerHierarchyTexts is a slice of strings for each container

onsi avatar Feb 17 '23 14:02 onsi

Thank you @onsi. Can you please point me to similar thing in v1.16.5

mkoppal-px avatar Feb 17 '23 18:02 mkoppal-px

You can use CurrentGinkgoTestDescription() - but I strongly recommend upgrading to V2. V1 is no longer supported and V2 has improved in many many ways. The migration guide is here and I'd be happy to help answer questions and resolve any issues you hit.

onsi avatar Feb 17 '23 19:02 onsi