How to measure Golang integration test coverage?

golang code coverage integration tests\
golang test coverage summary
go coverage with external tests
golang get overall test coverage
golang coverage report
golang test report html
golang test coverage exclude
golang coverage view

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.

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.

How to measure Golang integration test coverage?, In Golang, coverage rates are measured by overriding the package source code before compilation to add statistics and then compiling and  To measure the integration test coverage rate, you need to use the tools provided by Golang with flexibility and adapt test files based on the characteristics of your project code.

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

Measuring Integration Test Coverage Rates in PouchContainer , Your Golang tests might perform better than you think. of debugging and experimenting to find the arguments and execution orders. If you have a lot of integration tests, your code coverage might be higher than you think! Golang offers a unique way to generate a coverage binary instead of the default binary generated by go build. The binary for the code coverage which is generated writes a unique counter after every line of code and checks how many times this counter was called after the binary was executed.

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

How to get accurate code coverage in Golang (Go), Generating Coverage Profiles for Golang Integration Tests flags you'll find that go test has its own set of flags and flag parsing code. // the rest of test} Golang assert. For even easier Golang testing, there is a lesser known technique from the “testify” package. QA engineers tend to use “assert” for more complex projects. This allows them to write shorter test cases, and retrieve the same accurate results as provided by unit tests in Golang. For instance: package

Generating Coverage Profiles for Golang Integration Tests, Testing and measuring code coverage is one of the most important aspects of The go test command accepts a covermode flag to set the coverage mode to  Go Code Coverage. Go is one of the fastest growing programming languages. It is open source, freely available, and backed by Google. The popularity of Go can be gauged from the fact that it is now used in projects like Docker, Kubernetes and many more.

Easily Measure Go Code Coverage with Cover, Since version 1.2 code coverage is built into Go (Golang). Use go test -cover . to get basic coverage statistics for a single package. Integration (CI) and automatically track your code coverage for every single commit. In case  To ensure that integration tests run only when explicitly asked to, we need to tag the relevant test file. Notice line 3 of the web_test.go file: // +build integration. This line tags this test file and indicates that go should pick this file up only when supplied with the integration tag.

How to measure code coverage in Go, When you have integration tests that are wiring a bunch of things together, it's not uncommon to get suspiciously low test coverage scores when  Since version 1.2 code coverage is built into Go (Golang). Use go test -cover . to get basic coverage statistics for a single package. $ go test -cover . ok calc 0 .001s coverage: 100.0 % of statements

  • A useful utility for accurate code coverage reporting for Golang:
  • Thank you, Sergei. I was not aware of it, will check it out!
  • Great! I'm just linking this relevant golang-nuts thread.
  • Thanks, Simon. This is indeed what I was looking for!
  • 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.
  • @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!