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
- Compile your source code with
-fprofile-arcs -ftest-coverage, link against-lgcov. In addition to each.ofile, you will have a.gcnofile. - Run your program and cleanly exit it! This will produce a
.gcdafile for each source file. - Run
lcov --base-directory . --directory . --capture --output-file i3.infoto generate an info file. - Run
genhtml -o /tmp/i3-coverage i3.infoto 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
Did you like this post? Subscribe to this blog’s RSS feed to not miss any new posts!
I run a blog since 2005, spreading knowledge and experience for over 20 years! :)
If you want to support my work, you can buy me a coffee.
Thank you for your support! ❤️