Tuesday, March 08, 2005

C++ Test Coverage

I've been working on additional functional tests this week for my current project. Dan was working with me this morning when he asked about C++ test coverage tools. I have never used one for C++, so we did the obvious thing and googled for it.

Turns out that gcc comes with gcov.

1) Add the following option to the environment.

export CPPFLAGS="-fprofile-arcs -ftest-coverage"

2) Compile as normal
3) Run your tests.
4) gcov -l -c FileName.cpp
5) vi FileName.cpp.gcov and search for "######"

"######" indicate lines that were never executed.

It is simple and easy to use.

Here is an example of the output of gcov:

11: 9: for (i = 0; i < 10; i++)
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure\n");
-: 14: else
1: 15: printf ("Success\n");

Line #13 above was never executed. Line #9 was executed 11 times. You can also have it output percentages.

No comments: