Go's Test Coverage Tooling

February 8, 2022

Go Test Coverage Reports Are Awesome

Go has some awesome tooling around test coverage and reporting the coverage in a nice visual way. You can create a coverage profile produced by go test by running go test -coverprofile=c.out. This can then be used to generate coverage reports. You can then write out a nice visual HTML report of the coverage go tool cover -html=c.out -o coverage.html

Handy zsh alias

This gocover alias allows you to run the local go test coverage tooling and launch a browser to view the coverage report. This alias can be added to your ~/.zshrc:

For Linux:

alias www='python2 -m SimpleHTTPServer'

alias gocover=_gocover
function _gocover(){
    PKG=${PWD##*/}
    PKG_DIR=${PWD}
    MNT=/tmp
    echo "===> Running go test coverage tools: "
    echo ""
    go test -v -coverprofile $MNT/$PKG.coverage.out ./... || { echo ""; echo "======> Go Tests Failed"; return 1; }
    echo ""
    echo "===> Generating go test coverage report: "
    echo ""
    go tool cover -html=$MNT/$PKG.coverage.out -o $MNT/$PKG.coverage.htm || { echo ""; echo "======> Go Coverage Reports Failed"; return 1; }
    cd $MNT && www > /dev/null 2>&1 &
    WWW_PID=$!
    cd $PKG_DIR
    echo ""
    echo "===> Opening coverage report: "
    echo ""
    xdg-open "http://localhost:8000/$PKG.coverage.htm"
     echo "===> Type Q to kill www server"
    while read -k1 key
    do
      if [ "$key" = "Q" ];
      then
      kill -9 $WWW_PID
      break
      fi
    done
}

For MacOS:

alias www='python2 -m SimpleHTTPServer'

alias gocover=_gocover
function _gocover(){
    PKG=${PWD##*/}
    PKG_DIR=${PWD}
    MNT=/tmp
    echo "===> Running go test coverage tools: "
    echo ""
    go test -v -coverprofile $MNT/$PKG.coverage.out ./... || { echo ""; echo "======> Go Tests Failed"; return 1; }
    echo ""
    echo "===> Generating go test coverage report: "
    echo ""
    go tool cover -html=$MNT/$PKG.coverage.out -o $MNT/$PKG.coverage.htm || { echo ""; echo "======> Go Coverage Reports Failed"; return 1; }
    cd $MNT && www > /dev/null 2>&1 &
    WWW_PID=$!
    cd $PKG_DIR
    echo ""
    echo "===> Opening coverage report: "
    echo ""
    open "http://localhost:8000/$PKG.coverage.htm"
     echo "===> Type Q to kill www server"
    while read -k1 key
    do
      if [ "$key" = "Q" ];
      then
      kill -9 $WWW_PID
      break
      fi
    done
}

Using vscode

If you have the golang vscode extension installed, you can enable coloring of covered code in the editor panes gutter. You can add the following to your vscode settings.json to enable this:

    "go.coverOnSave": true,
    "go.coverageDecorator": {
        "type": "gutter",
        "coveredHighlightColor": "rgba(64,128,128,0.1)",
        "uncoveredHighlightColor": "rgba(128,64,64,0.1)",
        "coveredGutterStyle": "blockgreen",
        "uncoveredGutterStyle": "blockred"
    },
    "go.coverOnSingleTest": true,

I Repeat Go Test Coverage Reports Are Awesome

I hope you have learned that go allows for easy test coverage reporting out of the box.


comments powered by Disqus

Ⓒ 2020 Nathan Rockhold. All rights reserved.