QT Testing 2 – a Warm Welcome for Code Coverage

It is the most sexiest part of testing for me. You’ve made some unit tests for your project but you don’t know how much of your code you’ve tested or “covered” 🙂 It is out of your control and you’re about to loose your sanity.

At first gods have created Gcov , but with LCOV it is sexy, because it gives you graphical output with html results. It’s revolutionary!!! Project code is available on GitHub!

File:Human evolution scheme.svg - Wikipedia

Anyway, I feel like I have to list to steps to follow for coverage

  • Compiler has to know, it’s generating executable binary for code coverage, so some flags has to be set for it!
  • Executable binary has to run to generate some file extensions
  • lcov has to run to generate first result file
  • Results has to be filtered
  • Generating .html output to show off to people

Compiler Flags

Compiler flags are different from compiler to compiler. In this case I’m using GCC as my compiler. For LLVM it has different flags for profiling options. In root level of CMakeLists.txt file I’m appending this 2 lines:

add_compile_options(-fprofile-arcs -ftest-coverage)

add_compile_options adds our compiler options it’s like calling it : gcc -fprofile-arc -ftest-coverage. For coverage it also need to fprofile-generate libraries to be linked. In this step gcov is running as coverage tool!

Running executable binary

Whenever project has been build with this options, compiler will generate files with .gcno extension for each .o file . Whenever we run our executable binary it will generate .gcda file extensions.

LCOV part

LCOV Tool can get from here. It gives front end output to display. Actually I feel so tired to install from git. I’d suggest to install it’s portable version for automation. Time to time people can be desperate to add it into project repository or their toolchain, instead of installing to build agent lol 🙂 For this project as a lazy developer I used Manjaro’s application center and added to my environment path to call it directly. (btw lcov-git for manjaro get’s updates all the time from latest git snapshot)

Build output of project

at first I want to create a directory called coverage_results then run the test binary.

mkdir coverage_results

It’s time to give to lcov tool path of src files under build. lcov recursively search .gcda files under ./src path, so it’s important! if you give whole path it includes .gcda files under ./test path too and you will get wrong coverage result for your unit tests, because of you call almost every single line from your test code!

lcov -d ./src -c -o coverage_results/coverage-temp.info

Now if you give a direct generation of html results you’ll get unwanted results too. Let me show

genhtml -o coverage_results coverage_results/coverage-temp.info
Wrong coverage result

It is pointless to see /usr/include or QtCore or moc files generated by Qt itself. It has to be filtered!

You shall not pass | WANNART

I’m just removing this output files like nothing like this happened 🙂 and filter ./coverage_results/coverage-temp.info. At the beginning I called it coverage-temp on purpose 🙂

lcov -r coverage_results/coverage-temp.info "*Qt*" "*bits*"  -o coverage_results/coverage.info
genhtml -o coverage_results coverage_results/coverage.info
Clean result after filtering

lcov -r option removes file patterns and cleans out to a better result.

Coverage result

In Test project, I’ve used all methods that Calculation class provides. so I’m removing 2 of them to get a different coverage result to show off.

#include "test_math_calculations.h"
#include <QTest>
#include "calculations.h"
void TestMathCalculations::TestSum()
    Calculations calculations{};
    auto result = calculations.sum(1,3);
void TestMathCalculations::TestMultiply()
    Calculations calculations{};
    auto result = calculations.multiply(1,3);

Now doing whole process start over: Building project, running test executable binary and running LCOV, filtering it and will display html output to show off that I’m not covering whole class this time.

In conclusion, coverage tool shows to developer which part of code is covered or not. LCOV is a graphical way to use gcov and it’s FREE! 🙂 use while you can.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *