Wednesday, 22 October 2014

Qt + CMake + code coverage

Yesterday I was working on getting reports on code coverage for Injeqt library. I've found several tutorials on the internet, but every one of them was missing a detail or two.

So here is what I've come with:

1. Update compiler and linker flags in CMakeLists.txt file of your tests:
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W -g
    -fprofile-arcs -ftest-coverage -O0")
    -fprofile-arcs -ftest-coverage -O0")
    -fprofile-arcs -ftest-coverage -O0")
2. Link each test with gcov library:
target_link_libraries (test-target-name gcov)
3. Add new target for extracting coverage data:
add_custom_target (coverage
4. Insert script into directory of CMakeLists.txt:

lcov --capture --directory . --output-file
lcov --directory . --output-file \
    --remove "/usr/*" "*.moc" "test/*"
First lcov invocation will capture all data from previously executed tests. Second will remove useless data like coverage of generated moc files or of standard library headers and tests themselves.

5. Run tests and coverage:
make test
make coverage
6. Enjoy nice HTML coverage report.

Injeqt got 90,4% of lines coverage on first try! I hope to make it even better today.

No comments:

Post a Comment