Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang code coverage in integration tests?

It seems the point of "go tool cover -var=foo" may be to generate instrumented code that can be deployed in an integration test harness. Is anyone using this feature for that purpose? If so, is there a conventional way to dump the counters periodically? It seems the tricky part would be doing this across all files of interest with near simultaneity. Even then, some skew (if using -mode=count) would be unavoidable.

like image 244
kerlyn Avatar asked Jan 22 '15 04:01

kerlyn


1 Answers

We use this to collect code coverage from our various tests to get a single code coverage number across the tests and to see the uncovered code paths * Module tests * Integration tests * UI tests * API tests * unit tests

The way to achieve this is

  • Build an instrumented binary (app.debug) with coverage enables. The below command generates an app.debug with coverage instrumentation enabled

    $ go test -c -covermode=atomic -coverpkg="pkg/path/..." -o app.debug
    
  • Use this app.debug instead of your app in the tests and run tests against it. Ours is an HTTP server but this should work for most applications. Each test generates a seperate cov file which later needs to be merged.

    $ ./app.debug -test.coverprofile=functest.cov -- app.params
    
  • Merge all the test cov files to get a single cov file .For this, you can use gocovmerge

    $ find $COVERAGE_DIR -name *.cov | xargs gocovmerge > final.cov
    

And you have finally a coverage file which gives you complete picture of code coverage from all sorts of coverage.

like image 85
rajeshnair Avatar answered Oct 04 '22 00:10

rajeshnair