profile picture

Michael Stapelberg

Code Coverage testing in C with gcov and lcov (2011)

published 2011-05-08, last modified 2018-03-18
Edit Icon

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 i3.info to generate an info file.
  4. Run genhtml -o /tmp/i3-coverage i3.info 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 i3.info

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 i3.info file to a nice HTML report with the following command:

genhtml -o /tmp/i3-coverage/ i3.info

I run a blog since 2005, spreading knowledge and experience for almost 20 years! :)

If you want to support my work, you can buy me a coffee.

Thank you for your support! ❤️