Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go Coverage Report Integration with Jenkins

Tags:

go

jenkins

I wonder if there's any tool that can convert go test -coverprofile=cover.out into the formats that Jenkins can accept? I found some tools like go-junit-report and go2xunit, but they actually just convert output from go test -v, which is not the coverage report.

I want to know the detailed test coverage in Jenkins directly. Basically, I want to see the output from go tool cover -func=cover.out and go tool cover -html=cover.out in Jenkins webpage.

like image 883
J Freebird Avatar asked Jun 08 '16 18:06

J Freebird


People also ask

How does Jenkins check test coverage?

If you click on the Coverage Report icon, you will see code coverage for each package in your application, and even drill down to see the code coverage (or lack thereof) for an individual class (see Figure 2.31, “Jenkins lets you display code coverage metrics for packages and classes”).


3 Answers

There are several go tools for converting coverage data from go test to Cobertura for Jenkins: gocover-cobertura, or gocov with gocov-xml.

You can use gocover-cobertura as follows:

$ go get github.com/t-yuki/gocover-cobertura

$ go test -coverprofile=cover.out example.com/demo/...
ok      example.com/demo    0.008s  coverage: 0.0% of statements
ok      example.com/demo/cmd/demo   0.020s  coverage: 23.4% of statements

$ gocover-cobertura < cover.out > coverage.xml

$ head coverage.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage line-rate="0.35415787" branch-rate="0" version="" timestamp="1520609235359" lines-covered="839" lines-valid="2369" branches-covered="0" branches-valid="0" complexity="0">
        <sources>
                <source>/usr/local/go/src</source>
                <source>/Users/wilfred/workspace/go-scratch/src</source>
        </sources>
        <packages>
                <package name="example.com/demo/cmd/demo" line-rate="0.4848485" branch-rate="0" complexity="0">
                        <classes>

Note that you'll need Go 1.10+ to run -coverprofile against multiple packages in one run.

like image 78
Wilfred Hughes Avatar answered Oct 06 '22 11:10

Wilfred Hughes


https://github.com/AlekSi/gocov-xml

This is what we need. Use Coberuta plugin to generate the coverage profile.

like image 43
J Freebird Avatar answered Oct 06 '22 10:10

J Freebird


There isn't a dedicated plugin for Go coverage reports, nor really for generic code coverage.

For reports like this, I use the HTML Publisher Plugin to publish .html files created during a build.

like image 1
Christopher Orr Avatar answered Oct 06 '22 11:10

Christopher Orr