英文:
golang ginkgo test coverage across packages
问题
我有以下的文件结构:
❯ tree
.
├── go.mod
├── go.sum
├── Makefile
├── p1
│ └── p1.go
└── tests
└── integration
└── integration_suite_test.go
3 directories, 5 files
其中,p1/p1.go
文件中有一个函数:
❯ cat p1/p1.go
package p1
func MyTestFunction(s string) string {
return "hello " + s
}
我正在从不同目录的 ginkgo 测试中测试这个函数:
❯ 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))
})
})
})
我通过以下方式执行测试用例:
$ ginkgo -r -v -race --trace --cover --coverprofile=.coverage-report.out --coverpkg=./... ./...
但是 ginkgo 报告没有代码覆盖率,并且 .coverage-report.out
文件为空,尽管我已经指定了 --coverpkg
来包括所有目录。
❯ cat .coverage-report.out
mode: atomic
我的期望是错误的吗?在 ginkgo 中是否不可能实现跨包的代码覆盖率?还是我做错了什么?在这里,--coverpkg
似乎没有起到任何有用的作用。
英文:
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.
答案1
得分: 2
我在 ginkgo 仓库中提交了这个 问题,作者很友善地指出了解决这个问题的正确方法。
在调用 ginkgo
时,按照下面的方式启动,确保 --coverpkg
参数中指定了正确的完整路径(与你的 go.mod
文件中的路径一致),这样就可以正常工作了:
ginkgo -r -v -race --trace --coverpkg=example.com/test-ginkgo/p1 --coverprofile=.coverage-report.out ./...
现在我能够正确地看到 .coverage-report.out
文件了。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论