Code Coverage testing in C with gcov and lcov


This post is in english / Dieser Artikel ist auf Englisch, da er sich an die internationale Entwicklergemeinschaft richtet.

While the i3 window manager has a lot of testcases, I never got around to actually doing code coverage tests. Now I took the time to get it to work and want to describe the process so that others don’t do the same mistakes I did.

Outline of the process

  1. Compile your source code with -fprofile-arcs -ftest-coverage, link against -lgcov. In addition to each .o file, you will have a .gcno file.
  2. Run your program and cleanly exit it! This will produce a .gcda file for each source file.
  3. Run lcov --base-directory . --directory . --capture --output-file to generate an info file.
  4. Run genhtml -o /tmp/i3-coverage to convert that info file to HTML.

1: Changing the compilation flags

This step should be really straight-forward. Make sure the CFLAGS include -fprofile-arcs -ftest-coverage and the LDFLAGS include -lgcov:

CFLAGS += -fprofile-arcs -ftest-coverage
LDFLAGS += -lgcov

2: Run your program

Sounds easy, but be aware that you have to exit your program cleanly! Pressing Ctrl-C to abort it lead to a situation where no .gcda files were generated for me.

3: Run lcov (from CVS)

This one was tricky. At the time of writing, lcov’s most recent release is version 1.9. This version has a bug (it uses Perl’s two-parameter open) which leads to not opening the file <built-in>.gcov correctly. Get the most recent version from CVS, which includes a fix for this.

Afterwards, run the following command:

lcov --base-directory . --directory src --capture --output-file

The --base-directory parameter makes sure that relative filenames (like src/render.c) will be found.

Should you do multiple runs of your program, just repeat this command. If you are done and want to start over with fresh values, run lcov --directory . --zerocounters.

4: Run genhtml

The last step is to convert the file to a nice HTML report with the following command:

genhtml -o /tmp/i3-coverage/