Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure Golang integration test coverage?

Tags:

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.

like image 431
Anton Evangelatov Avatar asked Feb 05 '15 16:02

Anton Evangelatov


People also ask

How do I check my Golang coverage?

Try using gaia-docker/base-go-build Docker Image. This is a Docker image that contains all you need in order to build and test coverage. Running test coverage inside a Docker container creates . cover folder with test coverage results of your project.

Does code coverage include integration tests?

As a Developer or Tester, you can absolutely include Integration Testing in Code Coverage, if you have integrated code scripts.

How do you measure test coverage criteria?

You simply take: (A) the total lines of code in the piece of software you are testing, and. (B) the number of lines of code all test cases currently execute, and. Find (B divided by A) multiplied by 100 – this will be your test coverage %.


2 Answers

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.

like image 173
Anton Evangelatov Avatar answered Sep 24 '22 03:09

Anton Evangelatov


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
like image 24
Sharon KatzR7 Avatar answered Sep 21 '22 03:09

Sharon KatzR7