Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.2k views
in Technique[技术] by (71.8m points)

Per-test coverage in Go

I am building a Go provider for Pruner (a CLI that runs only the tests that ran through the lines you changed, saving you time).

For that, I need to be able to see per-test coverage. Not just a full coverage report after all tests have run, but I need a way to know which tests ran through what line.

Is that possible in Go?

I tried using -func, but it just gives me the method names of the original code, not the test code. In other words, I can't know what code each individual test runs through.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I need a way to know which tests ran through what line.

Is that possible in Go?

It's not supported by the tools. But you can do it. It's just very inefficient.

The way to do this is to run:

go test -cover -run=TheName/OfSome/SpecificTest

Then run this for each test in your suite.

Naturally, this will make your tests much more cumersome to manage, and incredibly slow.

So I would consider whether this is truly a requrement for your use case.

Go is optimized, from the ground-up, to compile quickly. If you have a Go project so long, that running all the tests is too slow, you may want to consider other alternatives. Some suggestions:

  1. Run more tests in parallel, so the total runtime is reduced.
  2. Take advantage of Short mode, and only run short tests by default, saving long-running tests for special cases.
  3. If you really need to run only a subset of tests, do it on a per-package basis, not on a per-test basis.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...