24

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.

Anton Evangelatov
  • 1,397
  • 2
  • 14
  • 31

4 Answers4

18

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.

Anton Evangelatov
  • 1,397
  • 2
  • 14
  • 31
2

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
1

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
Nick Craig-Wood
  • 52,955
  • 12
  • 126
  • 132
  • Thank you for your comment, but this is not exactly what I need. The problem was that the percentage of coverage does not cover the code in other packages of the project. I will update my question to reflect that and make it clearer. – Anton Evangelatov Feb 06 '15 at 10:40
  • @antonevangelatov I see what you mean! I'll leave this answer just in case anyone finds it useful, and I note `-coverpkg` in your answer, thanks! – Nick Craig-Wood Feb 06 '15 at 13:57
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
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250