Skip to content

gkampitakis/coverage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coverage

Coverage is a simple tool that checks per package coverage and fails if is not met.

go get github.com/gkampitakis/coverage

Usage

You can use it by calling in your TestMain. For more information around TestMain.

func TestMain(m *testing.M) {
	coverage.Run(m, 95)
}

What coverage.Run does, it runes the tests with t.Run() and then depending on the coverage fails the tests and calls os.Exit with correct exit code.

In case you have "clean" up code after running your tests coverage.Run support passing a callback function func() for running your code.

e.g.

func TestMain(m *testing.M) {
	coverage.Run(m, 91, func() {
		fmt.Println("tests are done")
	})
}

Example

PASS
coverage: 89.5% of statements

FAIL    Coverage threshold not met 91.0 >= 85.0 for my-package/example

FAIL    my-package/example        0.185s

coverage: 89.5% of statements reports slightly different number from the testing.Coverage. Couldn't find a way to fix this or an explanation.

coverage.Run will also fail when tests are run with no cover flag.

e.g.

PASS

FAIL    coverage is not enabled. You can enable by using `-cover`