How to determine code coverage of shared folders in GO?

My design structure
stuff/stuff.go -> package: stuff
test/stuff/stuff_test.go -> package: test
Although stuff_test executes code from stuff.go, it shows coverage: 0.0% of statements

i used go test -cover
If I transfer my * _test.go to the folder with the program files, it works fine.
Or maybe my approach to the design structure is poorly developed / agree?

+5
source share
2 answers

Cross-package coverage is not supported directly, but several people have built-in wrappers to combine individual coverage profiles.

See Issue No. 6909 for this story. And look at gotestcover for an example merge tool. There is also a gocovmerge . I built my own version, so I have not tried any of them, but I'm sure that they all work as mine, and mine work fine.

I mean, it's just a problem that no one wrote a really attractive list of changes and was not so important for the main developers, so it was not considered. This causes small corner cases that may disrupt existing tests, so the fast hacks that work for most of us have not been taken as is. But I did not see any discussion suggesting that the main developers are actively opposing this feature.

+3
source

The standard structure of a Go program holds tests with a package. Like this:

 project |-stuff |--stuff.go |--stuff_test.go 

At the top of the test files, you still declare package stuff , and your test methods are required to take the form TestMethodX if you want go test to run them automatically.

See Go docs for more details: https://golang.org/pkg/testing/

+3
source

Source: https://habr.com/ru/post/1239514/


All Articles