I wanted to find what my Unit Test coverage was like, so I started investigating code coverage tools.
My code is in C++, and I use UnitTest++ to test it. I develop on Unix.
There are quite a number of commercial tools, but I found the ones that come free with gcc work pretty well, once you figure out how to use them.
I'm using gcov in conjunction with lcov and genhtml to generate html pages with code coverage reports in them.
I've written a little shell script to do this for me. It's project specific, but you can probably modify it a little bit to work for you too:
1. #!/bin/bash 2. export CPPFLAGS="-fprofile-arcs -ftest-coverage" 3. lcov -d obj/i686-linux/ -b . -z 4. make clean;make 5. gcov -o obj/i686-linux/ * 6. lcov -d obj/i686-linux/ -b . -c -o output_lcov 7. genhtml output_lcov -o htmlfiles
Line 2 sets the instrumentation flags for g++. When you compile with these flags, g++ will generate files ending with the suffix .gcda and put them in the same place as your object (.o) files (it took me a while to find where this was).
Line 3 zeros out the counts from the previous run of the script. If you don't have this line, your statistics will accumulate each time you run your executable.
Line 4 rebuilds the source tree. Make sure you have built all your source with those flags, otherwise you may get error messages. My Makefile also runs the tests in the test directory below it. If yours doesn't, you'll need to run the binary you want to test coverage for at this point.
Line 5 runs gcov to generate statistics from the last run.
Line 6 generates a file genhtml can use to make pretty html files from it
Line 7 makes the pretty html files and puts them in the directory htmlfiles.
The -b argument in lcov and the second argument to gcov refer to the source files which are in the CWD.
