zig icon indicating copy to clipboard operation
zig copied to clipboard

support code coverage when testing

Open andrewrk opened this issue 7 years ago • 5 comments

Add a --coverage argument to zig test and have it generate a nice report.

andrewrk avatar May 02 '17 00:05 andrewrk

Based on my unpleasant experience with gcov (GCC coverage tool) in foolish drive for 100% coverage: there should be some easy way to label certain lines or whole functions as "doesn't need coverage". All kinds of unlikely situations, impossible to emulate.

gcov doesn't support anything like this, so I write a tool which scanned all C source files, searched for "no coverage" tags and removed these lines from gcov output.

It looked like:

if (...)  {
    exit(1); //--   (comment "//--" means no coverage for this line)
}

// The whole function is taken out of coverage via the //-->> and //<<-- brackets
void foo(void) { //-->>
  ...
} //<<--

There should be also some way to skip coverage checking for 3pp libraries, w/o touching their source code.

PavelVozenilek avatar Nov 10 '17 21:11 PavelVozenilek

Just though I'd mention that kcov works perfectly as is with zig at the moment. I think coverage in general is slightly more useful than it normally would be for zig as well, since it clearly shows what segments of code were never compiled. This helps with compile-time evaluation heavy bits.

Only steps required are:

zig test bigint.zig
kcov kcov-out ./zig-cache/test
firefox kcov-out/index.html

2018-05-17-192750_912x675_scrot

tiehuis avatar May 17 '18 07:05 tiehuis

Oh that's really interesting with the defers in yellow. The number tells you how many different ways there were to exit the block, and it only goes green if you manage to hit them all (in this case by having each try activate the error handling). Same thing with try, it'll be yellow unless you managed to test the error return.

The defer thing might change with #283

andrewrk avatar May 17 '18 14:05 andrewrk

I tried this using zig test on the std lib. It's really quite useful, but I see some confusing output. In my test results I see:

Test 341/466 os.path.join...OK
Test 342/466 os.path.isAbsoluteWindows...OK
Test 343/466 os.path.isAbsolutePosix...OK
Test 344/466 os.path.windowsParsePath...OK
Test 345/466 os.path.resolve...OK
Test 346/466 os.path.resolveWindows...OK
Test 347/466 os.path.resolvePosix...OK
Test 348/466 os.path.dirnamePosix...OK
Test 349/466 os.path.dirnameWindows...OK
Test 350/466 os.path.basename...OK
Test 351/466 os.path.relative...OK
Test 352/466 os.path.real...OK

but in the coverage report I see no coverage:

screenshot_2018-05-17_11-02-50

I wonder what's going on here.

andrewrk avatar May 17 '18 15:05 andrewrk

Looking forward to this option 🚀

piyushchauhan2011 avatar Jul 31 '22 05:07 piyushchauhan2011

kcov still works, the instructions have changed slightly as ./zig-cache/test doesn't exist.

rm -rf zig-cache/o
zig test lib/std/bit_set.zig
kcov kcov-out ./zig-cache/o/*/test
open kcov-out/index.html

It is very slow on my M1 Macbook Air. Took over 5 minutes.

Pyrolistical avatar Dec 09 '22 19:12 Pyrolistical

It would be interesting to generate a coverage report based on the kcov one* where the different logical possibilities and ramifications are informed in detail, as Jest does: (informing the coverage of "statements", "branch", "functions", "lines" and their logical ramifications)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |      100 |     100 |     100 |                   
 index.js  |     100 |      100 |     100 |     100 |                   
 server.js |     100 |      100 |     100 |     100 |                   
-----------|---------|----------|---------|---------|-------------------
Test Suites: 2 passed, 2 total
Tests:       11 passed, 11 total
Snapshots:   0 total
Time:        3.735 s, estimated 5 s
Ran all test suites.

*(kcov seems to provide everything that's necessary for this)

paulocoghi avatar Dec 24 '22 18:12 paulocoghi