-
Notifications
You must be signed in to change notification settings - Fork 0
Code Coverage
Zig doesn't have built-in code coverage support yet, but you can use external tools like kcov (Linux) or grindcov to generate coverage reports.
kcov uses DWARF debugging data from compiled programs to generate coverage information without requiring special compiler flags.
# Ubuntu/Debian
sudo apt-get install kcov
# Arch Linux
sudo pacman -S kcov
# From source
git clone https://github.com/SimonKagstrom/kcov.git
cd kcov && mkdir build && cd build
cmake .. && make && sudo make installRun your tests through kcov:
# Run ZSpec tests with coverage
kcov --include-pattern=/src/ coverage-output zig-out/bin/test
# Or use zig test directly with --test-cmd
zig build test --test-cmd kcov --test-cmd coverage-output --test-cmd-binFor projects using ZSpec, add a coverage step to your build.zig:
# Build tests first
zig build test
# Run with kcov
kcov --include-pattern=/src/ ./coverage ./zig-out/bin/testkcov generates an HTML report in the output directory:
# Open the coverage report
open coverage/index.html # macOS
xdg-open coverage/index.html # Linuxgrindcov uses Valgrind's Callgrind for instrumentation. It's slower but provides detailed coverage information.
# Install Valgrind first
sudo apt-get install valgrind
# Install grindcov
cargo install grindcovzig build test --test-cmd grindcov --test-cmd -- --test-cmd-binname: Coverage
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: '0.15.1'
- name: Install kcov
run: sudo apt-get install -y kcov
- name: Build tests
run: zig build test
- name: Run coverage
run: |
kcov --include-pattern=/src/ coverage ./zig-out/bin/test
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
directory: ./coverage
fail_ci_if_error: falseZig only compiles functions that are actually called/referenced. Completely unused functions don't contribute to the 'executable lines' total. This means:
- A file with one used function and many unused functions could show 100% coverage
- Results only indicate coverage of used functions
You can exclude specific lines from coverage:
# Using kcov's exclude option
kcov --exclude-line=//coverage:ignore coverage-output ./testThen in your code:
fn myFunction() void {
unreachable; //coverage:ignore
}A modified fork of kcov exists that automatically ignores Zig's unreachable and @panic statements.
To display a coverage percentage badge in your README:
Create a new public gist at https://gist.github.com with any content (it will be overwritten).
Copy the Gist ID from the URL (e.g., https://gist.github.com/username/abc123 → abc123).
- Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
- Generate a new token with
gistscope - Copy the token
In your repository settings:
- Add secret
GIST_TOKENwith your personal access token - Add variable
COVERAGE_GIST_IDwith your Gist ID
[](https://github.com/USERNAME/REPO/actions/workflows/coverage.yml)Replace USERNAME, GIST_ID, and REPO with your values.