Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: Wrong coverage when there is no tests for a package

I have a Go project with the following structure:

foo/foo.go
foo/foo_test.go
main.go

As you notice, there is no test for main.go.

I collect the coverage report using the following command:

go test ./foo ./ -coverprofile=coverage.txt -covermode=atomic

Here ./foo and ./ show where to look for packages.

Problem: I send the coverage report to codecov.io which shows that my code is 100% covered with tests. But this is not true as my main.go has no tests at all.

It seems like the system only counts those packages that explicitly specify test files.

Question: How to fix the coverage report in the way that it will count information about untested packages?

Note: You can find my project on GitHub and the real statistic is here. The project has a different structure, but the issue persists (wrong coverage).

like image 462
Sasha Shpota Avatar asked Jan 24 '20 20:01

Sasha Shpota


People also ask

What is the difference between code coverage and test coverage?

While code coverage helps you verify if each code in the software application is being executed by existing tests or not, test coverage indicates whether those tests are covering all the functional requirements of the application or not.

Is it possible to have 100% testing coverage explain why or why not?

100% test coverage simply means you've written a sufficient amount of tests to cover every line of code in your application. That's it, nothing more, nothing less. If you've structured your tests correctly, this would theoretically mean you can predict what some input would do to get some output.

What is acceptable test coverage?

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.

Should go tests be in the same package?

By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides. These files are not built by the compiler when you run the go build command, so you needn't worry about them ending up in deployments.


2 Answers

The -coverpkg flag may be used to specify the packages that are used as the basis of coverage analyis.

Quoting from Command go: Testing flags:

-coverpkg pattern1,pattern2,pattern3
    Apply coverage analysis in each test to packages matching the patterns.
    The default is for each test to analyze only the package being tested.
    See 'go help packages' for a description of package patterns.
    Sets -cover.

So in your specific example this will do it:

go test -coverpkg=.,./foo -coverprofile=coverage.txt -covermode=atomic . ./foo

To apply it for a complete module / project, you may use:

go test -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic ./...

Another option is to place an "empty" test file into the folders of packages that do not currently have a test file. That way they will be naturally included in default coverage analysis, but obviously nothing will be covered from them.

See related discussion on github:

cmd/go: go test -cover & go test -coverprofile should always output a coverage #24570

like image 143
icza Avatar answered Oct 01 '22 22:10

icza


try this one:

go test -coverpkg=./... -race -coverprofile=coverage.txt -covermode=atomic ./..

enter image description here

like image 45
Prakash Kumar Avatar answered Oct 01 '22 21:10

Prakash Kumar