Write unit tests by mocking and measure your test coverage

Two (that I know of) excellent resources for learning how to keep on top of your Go tests came out this week:

  1. Fast Unit Tests with Mocking, by @arschles:
    https://www.youtube.com/watch?v=mk4BCLimksY&feature=youtu.be

  2. MEASURING FULL TEST COVERAGE OF INTEGRATION TESTS IN A PROJECT USING -COVERPKG, by @kytrinyx
    http://whipperstacker.com/2015/10/01/measuring-full-test-coverage-of-integration-tests-in-a-project-using-coverpkg/

You’re welcome.

1 Like

Here are two bash functions that I use to check coverage

cover () { 
  t=$(tempfile)
  go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -func=$t && unlink $t
}

cover-web() {
  t=$(tempfile)
  go test $COVERFLAGS -coverprofile=$t $@ && go tool cover -html=$t && unlink $t
}

The first will run the tests for the current package and print out a per function coverage report, the second runs the tests then opens your web browser to the coverage report.

Hope this is useful.

10 Likes

Not even aware of those, big appreciate!

Cool. Though I would have assumed some ‘gb’ usage in there… :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.