Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage for cross package tests #995

Closed
psankar opened this issue Jun 13, 2022 · 5 comments
Closed

Code coverage for cross package tests #995

psankar opened this issue Jun 13, 2022 · 5 comments

Comments

@psankar
Copy link

psankar commented Jun 13, 2022

I reported this issue in stackoverflow initially but since I did not get an answer there, I wanted to bring it up here so that I would know if this is not a supported usecase.

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 here.

@onsi
Copy link
Owner

onsi commented Jun 14, 2022

hey there - I believe the issue is that --coverpkg does not support ./.... Ginkgo does not implement it's own coverage reporting - it's simply using what go test does under the hood. This blog post discusses Go's coverage support and this blog post talks about coverpkg specifically.

In your particular case, try

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

@psankar
Copy link
Author

psankar commented Jun 14, 2022

@onsi Thanks a lot. Explicitly mentioning the package in the --coverpkg worked but the relative path did not work, I had to give the absolute package path.

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

I believe that this bug can be closed, but if you can update the docs for this particular case, it would be nice. Thank you once again for the helpful library and your comments on the issues.

@thetruechar
Copy link

it's so strange, why
ginkgo --cover -covermode=count --coverpkg=test-example/server/... -coverprofile=c.out -r works
ginkgo --cover -covermode=count --coverpkg=test-example/server/ -coverprofile=c.out -r not works
ginkgo --cover -covermode=count --coverpkg=./server -coverprofile=c.out -r not works
ginkgo --cover -covermode=count --coverpkg=./server/... -coverprofile=c.out -r not works

@onsi
Copy link
Owner

onsi commented Nov 16, 2022

I think I understand the issue here. relative paths don't work with -r because ginkgo changes the working directory to the test suite and then runs the go test command - but the relative path will be wrong in that working directory.

I can probably fix that by translating relative paths to absolute paths and then running with those. I'll need to give it a try when I next set aside time to work on ginkgo.

@onsi
Copy link
Owner

onsi commented Mar 12, 2023

this should now be fixed in v2.9.1 - ginkgo -coverpkg=./... -r will now compute coverage for all code by all tests.

@onsi onsi closed this as completed Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants