srikant
srikant

Reputation: 260

Go Test - empty coverage file

I have a go project, test directory contains a single sample test file. Few Functions of the test file are

func TestMain(m *testing.M) {
    setup()
    code := m.Run()
    shutdown()
    os.Exit(code)
}
func TestUserLogin(t *testing.T) {
    //sample code to make api call and validate response
}
func setup() {
    //start service
}
func shutdown() {
    //stop service
}

I want to see/generate a coverage report. The following command is being used to run tests:

go test -coverprofile=coverage.out

Output on terminal is

PASS
coverage: 100.0% of statements

coverage.out is getting generated but it consists of only single line

mode: set

coverage.out should have information about the files, lines, etc.

Anything that I am doing wrong here?

Upvotes: 1

Views: 2122

Answers (2)

Yuseferi
Yuseferi

Reputation: 8720

in general tests and codes should be in the same package to generate coverage report. if not you need to pass -coverpkg=./... .

for example:

 go test -race -p 1 -coverpkg=./... ./... -coverprofile=cover.out

Upvotes: 1

Cyan Tarek
Cyan Tarek

Reputation: 84

test directory contains a single sample test file

Here it is, Go coverage does not work if your *_test.go files are not in the same directory a.k.a they have been put in another folder.

Upvotes: 1

Related Questions