Cover report in multiple packages

2,155 views
Skip to first unread message

tgros...@extrahop.com

unread,
Aug 19, 2014, 5:49:24 PM8/19/14
to golan...@googlegroups.com
I have an application with a few packages, and unit tests mostly located in just one package. For example:

A -> B -> C

My unit tests run against the code in package A which depends on package B which depends on package C. When running with "-cover" I only get a report of the coverage in package A. Is there a way to get the coverage report for all the packages in the project based on the tests that are run?

Chris Kastorff

unread,
Aug 19, 2014, 11:50:47 PM8/19/14
to golan...@googlegroups.com
> My unit tests run against the code in package A which depends on package
> B which depends on package C. When running with "-cover" I only get a
> report of the coverage in package A. Is there a way to get the coverage
> report for all the packages in the project based on the tests that are run?

From go test -help:
-coverpkg pkg1,pkg2,pkg3
Apply coverage analysis in each test to the given list of
packages. The default is for each test to analyze only the package being
tested. Packages are specified as import paths.

E.g. in the directory for package A, run "go test -coverpkg A,B,C".

-Chris K

Vivekanand S V

unread,
Aug 20, 2014, 12:29:57 AM8/20/14
to golang-nuts
Is there any way to automatically include the dependent packages ? I don't see that mentioned in the help.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fatih Arslan

unread,
Aug 20, 2014, 11:29:59 AM8/20/14
to golang-nuts
On 08/20, Vivekanand S V wrote:
> Is there any way to automatically include the dependent packages ? I don't
> see that mentioned in the help.


DEPS=$(go list -f $'{{range $f := .Imports}}{{$f}},{{end}}')
go test -coverpkg=$DEPS

This will include all your imports. The output will be like:

PASS
coverage: 19.5% of statements in errors, fmt, reflect, strings,
ok github.com/fatih/structs 0.008s


--
Fatih Arslan

Fatih Arslan

unread,
Aug 20, 2014, 11:31:57 AM8/20/14
to golang-nuts
On 08/20, Fatih Arslan wrote:
> DEPS=$(go list -f $'{{range $f := .Imports}}{{$f}},{{end}}')

Also, change "Imports" to "Deps" to get all recursive dependencies

DEPS=$(go list -f $'{{range $f := .Deps}}{{$f}},{{end}}')


--
Fatih Arslan

Vivekanand S V

unread,
Aug 25, 2014, 1:22:03 AM8/25/14
to golang-nuts
Thanks, it was very helpful. I am trying to modify it to output only a set of packages starting with some prefix. Hopefully i'll get it done soon :)


Thanks once again.




--
Fatih Arslan

julio....@gmail.com

unread,
May 23, 2016, 2:15:30 AM5/23/16
to golang-nuts
Is it possible to do the same at run-time?
When running tests recursively from a root directory, it is kind of weird to make such coverpkg list... It would be much cleaner to write this list in the test file instead or to offer better `-coverpkg` values...

Henry

unread,
May 23, 2016, 10:58:10 PM5/23/16
to golang-nuts, julio....@gmail.com
I usually run my own tool to check for code coverage in a project. It is a thin wrapper built on top of Go's standard testing tool. What it does is -given a root directory- it will recursively run go test against the sub-directories. It will show the packages with less than 100% coverage. Otherwise, it remains silent. It skips the 'package main', test files, empty directories, and other non-relevant stuffs.   

I am not sure if it is what you are looking for, but if you need it you can get it at github.com/henryas/coverall. I usually use it as a part of a larger tool chain, which is why I need it to remain silent when it has no problem.

Dave MacFarlane

unread,
May 24, 2016, 9:31:52 AM5/24/16
to julio....@gmail.com, golang-nuts
If all you're trying to do is get coverage of all your subpackages for a CI and aren't concerned about downstream packages or manually toggling which ones are one/off at runtime, I've used this script:


with Travis, and it seems to work fine.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
- Dave

Kn

unread,
Apr 2, 2021, 7:23:52 AM4/2/21
to golang-nuts
 Add option `-coverpkg=$(go list ./...|sed 's/ /,/g')` works.
Reply all
Reply to author
Forward
0 new messages