Skip to content

Instantly share code, notes, and snippets.

@KunalSin9h
Created February 4, 2023 16:41
Show Gist options
  • Save KunalSin9h/1e71579764c46c969305131bd60bd309 to your computer and use it in GitHub Desktop.
Save KunalSin9h/1e71579764c46c969305131bd60bd309 to your computer and use it in GitHub Desktop.
Build in ways to check the code coverage in golang!
@KunalSin9h
Copy link
Author

KunalSin9h commented Feb 4, 2023

  1. Build the source with -cover flag
    ex.
$ go build -cover main.go
  1. Create a coverage dir to store the coverage data.
    ex.
$ mkdir coverage
  1. Run the go binary with GOCOVERDIR env variable, point to coverage dir
    ex.
$ GOCOVERDIR=coverage ./main

The above step put the coverage data into coverage dir

See the Code Coverage Result using html

  1. go tool covdata textfmt -i=coverage -o profile.txt
  2. go tool cover -html=profile.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment