GPROF Test Coverage Script
-
Book Excerpt from "Generative AI in C++" (full text online)
-
by David Spuler, Ph.D.
GPROF Test Coverage Script
There are many software development tools available to do testing coverage. But here's an easy way to do function-level test coverage using GCC and GPROF on Linux. GPROF is a performance profiling tool that's intended to help you identify CPU usage costs across your code, but it can also be repurposed to help with test coverage.
The method is basically:
- Re-compile your program with
g++using the “-pg” options (debug mode) - Run your program in test mode (i.e. runs the unit test harness)
- After execution, there's a binary file “
gmon.out” in the current directory. - Run
gprofto process “gmon.out” and generate a profiling report (in human-readable text format) - Use a script to extract the “uncalled functions” section in the report (e.g. use “
awk”) - Then you have a list of functions not “covered” by unit tests.
Personally, I build all of these steps into a Makefile,
so I can run my own command like “make coverage”,
but you can also script it separately.