-1

I have just 24h working with Go so I am super new to this.

I'm trying to get a complete test conversion but when I run

go test -cover new-project/anto/... .

in new-project/anto/mon it shows me that I have 51.8% coverage but in reality I have more coverage because there are tests that are tested in new-project/anto/integration where I have a test file that I import the new-project/anto/mon for testing.

The thing is that as I was reading Golang only takes the tests if they are in the same folder but having separate files does not detect it correctly.

I was wondering if there is a way to get the full coverage.

go test -cover new-project/anto/... .
?       new-project/anto/       [no test files]
ok      new-project/anto/conf   6.060s  coverage: 73.1% of statements
ok      new-project/anto/integration    43.309s coverage: [no statements]
?       new-project/anto/integration/conf   [no test files]
?       new-project/anto/integration/mocks  [no test files]
?       new-project/anto/integration/utils  [no test files]
ok      new-project/anto/logs   0.024s  coverage: 70.5% of statements
ok      new-project/anto/metric 0.012s  coverage: 59.0% of statements
ok      new-project/anto/mon    1.612s  coverage: 51.8% of statements
?       new-project/anto/anotherComponents  [no test files]

Doing some research, I found a go-acc plugin that validates all the packages, the curious thing is that mon instead of having 51.8% now has 29.8%.

go-acc new-project/anto/... .
?       new-project/anto/   [no test files]
ok      new-project/anto//conf  5.030s  coverage: 73.1
ok      new-project/anto//integration   43.117s coverage: 73.5
?       new-project/anto//integration/conf  [no test files]
?       new-project/anto//integration/mocks [no test files]
?       new-project/anto//integration/utils [no test files]
ok      new-project/anto//logs  0.008s  coverage: 22.3
ok      new-project/anto//metric    0.020s  coverage: 59.0
ok      new-project/anto//mon   1.460s  coverage: 29.8
?       new-project/anto//anotherComponents     [no test files]
?       new-project/anto/   [no test files]

1 Answer 1

9

The first command will generate coverage.out for all packages. The second parses the coverage.out and outputs the results:

go test ./...  -coverpkg=./... -coverprofile ./coverage.out
go tool cover -func ./coverage.out

Sample output:

golang.org/x/example/stringutil/reverse.go:21: Reverse      100.0%
total:                                         (statements) 100.0%

For more details see the various help outputs:

  • go help test
  • go help testflags
  • go help packages
  • go tool cover --help
1
  • Hello Matthew Warman, thanks a lot for your help! but when I try this option all my test are failure go test ./... -coverpkg=./... -coverprofile ./coverage.out ? new-project/anto [no test files] FAIL new-project/anto/conf [build failed] FAIL new-project/anto/integration 0.012s ? new-project/anto/integration/conf ? new-project/anto/integration/mocks ? new-project/anto/integration/utils FAIL new-project/anto/logv FAIL new-project/anto/metric FAIL new-project/anto/mon go tool cover -func ./coverage.out total: (statements) 0.0%
    – AntoCode
    Sep 7, 2021 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.