How to get code coverage using zig and kcov

May 15, 2020


Here's a short post on how to get code coverage with zig using codecov on travis CI.


The kcov project works pretty great with zig. In the after_success block (depending on what CI you're using) just add the docker command to run kcov on the test files.



language: cpp
dist: bionic
services:
    - docker

before_install:
    # Install zig
    - sudo snap install zig --classic --edge

script:
    # Test request parser
    - zig test kiwi.zig

after_success:
    - docker run --rm --security-opt seccomp=unconfined -v $(pwd):$(pwd) -w $(pwd) kcov/kcov kcov kcov-out ./zig-cache/o/*/test
    - bash <(curl -s https://codecov.io/bash) -s kcov-out


The command maps the host pwd to the same path in docker container to make codecov detect the paths correctly.

Note: The --security-opt seccomp=unconfined is needed for kcov to run in docker (see this issue)


Also see kcov codecov docs and zig coverage issue.


It's not perfect since it counts test blocks as part of the coverage and runs the tests twice... but it's really helpful for showing what was inlined, and what wasn't covered by the tests!


Hope this helps someone!