How to get code coverage of a library from unit tests

Last edited on

We suppose that the project contains N libraries in the directories libprojN. Each library generated are static libraries, and they are generated in libprojN. We have N unit tests, all of them are located in testprojM directories.

So the directory tree looks as follows:

libproj1
  |   libproj1.a
  +-- libproj1.a.csmes
libproj2
  |   libproj2.a
  +-- libproj2.a.csmes
......
libprojN
  |   libprojN.a
  +-- libprojN.a.csmes

testproj1
  |   testproj1.exe
  +-- testproj1.exe.csmes
testproj2
  |   testproj2.exe
  +-- testproj2.exe.csmes
.....
testprojM
  |   testprojM.exe
  +-- testprojM.exe.csmes

First we need to execute the unit tests:

for TESTDIR in testproj*
do
   cd $TESTDIR
   ./$TESTDIR.exe
   cd ..
done

After execting this bash script, a .csexe file is created for each unit test:

testproj1
  |   testproj1.exe
  |   testproj1.exe.csexe
  +-- testproj1.exe.csmes
testproj2
  |   testproj2.exe
  |   testproj2.exe.csexe
  +-- testproj2.exe.csmes
.....
testprojM
  |   testprojM.exe
  |   testprojM.exe.csexe
  +-- testprojM.exe.csmes

We import then each testprojM.exe.csexe file into its testprojM.exe.csmes file:

for TESTDIR in testproj*
do
   cmcsexeimport -m $TESTDIR/$TESTDIR.exe.csmes -e $TESTDIR/$TESTDIR.exe.csexe \
                 --delete --title="$TESTDIR"
done

Now, the files testprojM.exe.csmes contain the coverage information of the unit tests and the libraries that were tested. The files testprojM.exe.csexe are deleted because they are no longer needed.

It is now possible to import the coverage information of the unit tests into the .csmes file of each tested library:

TESTPROJs=$(ls testproj*/testproj*.exe.csmes)
for LIBRARY_DIR in libproj*
do
   LIBPROJ=$LIBRARY_DIR/$LIBRARY_DIR.a.csmes
   cmmerge -a -o $LIBPROJ -i $LIBPROJ $TESTPROJs
done

This script imports into each file libprojN.a.csmes all its unit test results. The -i command line option lets cmmerge ignore the source files which are not present in libprojN.a.csmes. This way, the test code is ignored during the merge process.