2 ms·
There was a technique that existed already where you could use `go test -cover` and the `-o` flag to produce a binary from `go test` rather than actually runnin
by eatonphil 4y ago
There was a technique that existed already where you could use `go test -cover` and the `-o` flag to produce a binary from `go test` rather than actually running tests. So you could build a binary that had coverage enabled. Then when you exercised code paths by running that binary with whatever inputs, coverage would be collected and dumped into some local file.
Here's an example of the build step: https://github.com/multiprocessio/datastation/blob/main/runner/scripts/runner_test.build#L3 https://github.com/multiprocessio/datastation/blob/main/runn....
And here's how I'd analyze coverage after using the binary: https://github.com/multiprocessio/datastation/blob/main/runner/scripts/test_coverage.sh https://github.com/multiprocessio/datastation/blob/main/runn.... This analysis actually combined both unit test coverage files with integration test coverage so it gave a pretty good overall indication of code paths covered by any tests.
I can't remember where I found this technique but it's been around for a while.
This new option is the same thing but a way to `go build` with `-cover` instead of `go test -cover -o $out`? Do I have that right?
Another example of doing this from 2019: https://stackoverflow.com/a/55640874/1507139 https://stackoverflow.com/a/55640874/1507139. I'm sure I learned about this from somebody's blog but I can't remember where.
- FiloSottile 4y agoNo idea if the technique was original, it’s been too long, but I wrote about it on the Cloudflare blog in 2016. https://blog.cloudflare.com/go-coverage-with-external-tests/ https://blog.cloudflare.com/go-coverage-with-external-tests/ Anyway, very happy to see the team ship first class support for it. It immediately made coverage support in testscript much more natural, too. https://github.com/rogpeppe/go-internal/pull/201 https://github.com/rogpeppe/go-internal/pull/201
- aleksi 4y agoElastic wrote about that in 2015: https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests https://www.elastic.co/blog/code-coverage-for-your-golang-sy.... I guess multiple people figured it out independently.
- eliben 4y agoYes, technically you could wrap every `main` in a test and build everything just right, but it's nice to be able to do this without any hacks if you already have an integration test suite and you want to know what coverage it gets you.