golang ginkgo test coverage across packages

Issue

I have the following file structure:

❯ tree
.
├── go.mod
├── go.sum
├── Makefile
├── p1
│   └── p1.go
└── tests
    └── integration
        └── integration_suite_test.go

3 directories, 5 files

Where, the p1/p1.go has a function:

❯ cat p1/p1.go
package p1

func MyTestFunction(s string) string {
        return "hello " + s
}

which I am testing from a ginkgo test from a different directory:

❯ cat tests/integration/integration_suite_test.go 
package integration_test

import (
        "testing"

        "example.com/test-ginkgo/p1"
        . "github.com/onsi/ginkgo/v2"
        . "github.com/onsi/gomega"
)

func TestIntegration(t *testing.T) {
        RegisterFailHandler(Fail)
        RunSpecs(t, "Integration Suite")
}

var _ = Describe("Testing external function", func() {
        _ = Context("from integration test", func() {
                It("and verify coverage", func() {
                        input := "test"
                        want := "hello " + input
                        got := p1.MyTestFunction(input)
                        Expect(got).To(Equal(want))
                })
        })
})

I execute the cases via:

$ ginkgo -r -v -race --trace --cover --coverprofile=.coverage-report.out --coverpkg=./... ./...

but ginkgo reports that no code coverage and the .coverage-report.out file is empty, even though I have specified --coverpkg to include all the directories.

❯ cat .coverage-report.out 
mode: atomic

Is my expectation wrong and such a coverage across packages not possible with ginkgo ? Or am I doing something wrong ? The --coverpkg seems like not doing anything useful here.

Solution

I filed this as a issue in the ginkgo repo and the author was graceful enough to point me to the correct way to solve this.

While calling ginkgo launch it as below, with the correct full path
(as mentioned in your go.mod file) mentioned for the --coverpkg parameter and it would work:

ginkgo -r -v -race --trace --coverpkg=example.com/test-ginkgo/p1 --coverprofile=.coverage-report.out ./...

Now I am able to see the .coverage-report.out containing correctly.

Answered By – Sankar

Answer Checked By – Mildred Charles (GoLangFix Admin)

Leave a Reply

Your email address will not be published.