如何测量Golang集成测试覆盖率?

huangapple go评论93阅读模式
英文:

How to measure Golang integration test coverage?

问题

我正在尝试使用go test -cover来测量我正在构建的服务的测试覆盖率。这是一个REST API,我通过启动它、发送测试HTTP请求并查看HTTP响应来进行测试。这些测试不是服务包的一部分,因此go tool cover返回的测试覆盖率为0%。有没有办法获得实际的测试覆盖率?我期望在给定的端点上进行最佳情况下的测试,至少覆盖特定端点处理程序代码的30-50%,通过添加更多针对常见错误的测试来进一步提高覆盖率。

英文:

I am trying to use go test -cover to measure the test coverage of a service I am building. It is a REST API and I am testing it by spinning it up, making test HTTP requests and reviewing the HTTP responses. These tests are not part of the packages of the services and go tool cover returns 0% test coverage. Is there a way to get the actual test coverage? I would expect a best-case scenario test on a given endpoint to cover at least 30-50% of the code for specific endpoint handler, and by adding more tests for common error to improve this further.

答案1

得分: 18

我被指向了-coverpkg指令,它可以实现我需要的功能-测量特定包中的测试覆盖率,即使使用该包的测试不属于该包。例如:

$ go test -cover -coverpkg mypackage ./src/api/...
ok      /api    0.190s  coverage: 50.8% of statements in mypackage
ok      /api/mypackage   0.022s  coverage: 0.7% of statements in mypackage

$ go test -cover ./src/api/...
ok      /api    0.191s  coverage: 71.0% of statements
ok      /api/mypackage   0.023s  coverage: 0.7% of statements

在上面的示例中,我在main_test.go中有一些测试,它属于package main,而package mypackage则被package main使用。我主要关注package mypackage的覆盖率,因为它包含项目中99%的业务逻辑。

我对Go还比较新,所以通过集成测试来测量测试覆盖率可能不是最佳方法。

英文:

I was pointed at the -coverpkg directive, which does what I need - measures the test coverage in a particular package, even if tests that use this package and not part of it. For example:

$ go test -cover -coverpkg mypackage ./src/api/...
ok      /api    0.190s  coverage: 50.8% of statements in mypackage
ok      /api/mypackage   0.022s  coverage: 0.7% of statements in mypackage

compared to

$ go test -cover ./src/api/...
ok      /api    0.191s  coverage: 71.0% of statements
ok      /api/mypackage   0.023s  coverage: 0.7% of statements

In the example above, I have tests in main_test.go which is in package main that is using package mypackage. I am mostly interested in the coverage of package mypackage since it contains 99% of the business logic in the project.

I am quite new to Go, so it is quite possible that this is not the best way to measure test coverage via integration tests.

答案2

得分: 2

你可以以生成覆盖率 HTML 页面的方式运行 go test,就像这样:

go test -v -coverprofile cover.out ./...
go tool cover -html=cover.out -o cover.html
open cover.html
英文:

you can run go test in a way that creates coverage html pages. like this:

go test -v -coverprofile cover.out ./...
go tool cover -html=cover.out -o cover.html
open cover.html

答案3

得分: 1

据我所知,如果你想要测试覆盖率,你需要运行go test -cover命令。

然而,你可以很容易地添加一个标志,通过传递该标志来启用额外的测试,这样你就可以将它们作为测试套件的一部分,但通常不运行它们。

所以在你的whatever_test.go文件中添加一个命令行标志:

var integrationTest = flag.Bool("integration-test", false, "Run the integration tests")

然后在每个测试中做如下处理:

func TestSomething(t *testing.T){
    if !*integrationTest {
        t.Skip("Not running integration test")
    }
    // 进行一些集成测试
}

然后运行集成测试:

go run -cover -integration-test
英文:

As far as I know, if you want coverage you need to run go test -cover.

However it is easy enough to add a flag which you can pass in which will enable these extra tests, so you can make them part of your test suite but don't run them normally.

So add a command line flag in your whatever_test.go

var integrationTest = flag.Bool("integration-test", false, "Run the integration tests")

Then in each test do something like this

func TestSomething(t *testing.T){
    if !*integrationTest {
        t.Skip("Not running integration test")
    }
    // Do some integration testing
}

Then to run the integration tests

go run -cover -integration-test

答案4

得分: 0

2015年:

这些测试不是服务包的一部分,go工具的cover命令返回0%的测试覆盖率。

2023年3月,Go 1.20 Cover 添加了以下内容:

Go 1.20支持收集程序(应用程序和集成测试)的代码覆盖率文件,而不仅仅是单元测试。

要收集程序的覆盖率数据,可以使用go build命令的-cover标志构建程序,然后使用环境变量GOCOVERDIR设置为覆盖率文件的输出目录来运行生成的二进制文件。
有关如何入门的更多信息,请参阅“集成测试的覆盖率”页面。有关设计和实现的详细信息,请参阅提案

根据文档“Go集成测试的代码覆盖率”:

在1.20版本中,Go的覆盖率工具不再局限于包测试,而是支持从更大的集成测试中收集覆盖率文件。

示例:

$ go build -cover -o myprogram.exe myprogram.go
$ mkdir somedata
$ GOCOVERDIR=somedata ./myprogram.exe
我说“Hello, world.”和“see ya”
$ ls somedata
covcounters.c6de772f99010ef5925877a7b05db4cc.2424989.1670252383678349347
covmeta.c6de772f99010ef5925877a7b05db4cc

并且

使用覆盖率数据文件

Go 1.20引入了一个新工具“covdata”,用于读取和操作来自GOCOVERDIR目录的覆盖率数据文件。

$ go tool covdata percent -i=somedata
    main    覆盖率:100.0%的语句
    mydomain.com/greetings  覆盖率:100.0%的语句
英文:

2015:

> These tests are not part of the packages of the services and go tool cover returns 0% test coverage

March 2023, Go 1.20 Cover adds:

> Go 1.20 supports collecting code coverage profiles for programs (applications and integration tests), as opposed to just unit tests.
>
> To collect coverage data for a program, build it with go build's -cover flag, then run the resulting binary with the environment variable GOCOVERDIR set to an output directory for coverage profiles.
> See the 'coverage for integration tests' landing page for more on how to get started. For details on the design and implementation, see the proposal.

As documented in "Code coverage for Go integration tests" with

> With the 1.20 release, Go’s coverage tooling is no longer limited to package tests, but supports collecting profiles from larger integration tests.

Example:

$ go build -cover -o myprogram.exe myprogram.go
$ mkdir somedata
$ GOCOVERDIR=somedata ./myprogram.exe
I say "Hello, world." and "see ya"
$ ls somedata
covcounters.c6de772f99010ef5925877a7b05db4cc.2424989.1670252383678349347
covmeta.c6de772f99010ef5925877a7b05db4cc

And

> ## Working with coverage data files
>
> Go 1.20 introduces a new tool, 'covdata', that can be used to read and manipulate coverage data files from a GOCOVERDIR directory.

$ go tool covdata percent -i=somedata
    main    coverage: 100.0% of statements
    mydomain.com/greetings  coverage: 100.0% of statements

huangapple
  • 本文由 发表于 2015年2月6日 00:27:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/28349085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定