How to generate code coverage report - Cidana-Developers/Cidana-Task-Manager GitHub Wiki

Generate code coverage report

Code coverage is an important requirement for our unit test project, here we will introduce the step briefly. We will take libaom as an example.

Use gcov and lcov to generate code coverage report

  1. compile:

    1. using gcc and g++: cmake ../aom_mod/ -DCMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_CXX_COMPILER=/usr/bin/g++
    2. add following flags -fprofile-arcs -ftest-coverage
    3. add link flags: -fprofile-arcs -ftest-coverage
    4. build debug mode
    5. total command:
    cmake ../aom_mod/ -DCMAKE_C_COMPILER=/usr/bin/gcc
       -DCMAKE_CXX_COMPILER=/usr/bin/g++ 
       '-DAOM_EXTRA_C_FLAGS=-fprofile-arcs -ftest-coverage' 
       '-DAOM_EXTRA_CXX_FLAGS=-fprofile-arcs -ftest-coverage' 
       '-DAOM_EXTRA_EXE_LINKER_FLAGS=-fprofile-arcs -ftest-coverage' -DCMAKE_BUILD_TYPE=Debug
    1. check the files generated: find name with suffix gcno or gcda, usually they are generated in the same folder with obj files.
  2. clean up from previous run

$ lcov --zerocounters --directory .
  1. create baseline
$ lcov --no-external --capture --initial --base-directory ../aom_mode --directory . --output-file
   libaom_base.info
  1. run test
$ ./test --gtest_filter="test.*"
  1. run lcov again after tests
$ lcov --no-external --capture --base-directory ../aom_mode
   --directory . --output-file libaom_test.info
  1. combine lcove tracefiles
$ lcov --add-tracefile ./libaom_base.info --add-tracefile
   ./libaom_test.info --output-file ./libaom_total.info
  1. remove/filter out remaining unwanted stuff
$ lcov -r libaom_total.info "*third_party*" -o
   libaom_total_reduced.info
  1. Generate html
$ genhtml decoder.info --output-directory ./
  1. Generate report for jenkins Install lcov_cobertura.py
$ pip install lcov_cobertura.py

Find where is lcov_cobertura, open python console

In [0]: import lcov_cobertura
In [1]: lcov_cobertura.__file__ # will output the location of lcov_cobertura.pyc

create cobertura report:

$ python /home/wenyao/.local/lib/python2.7/site-packages/lcov_cobertura.pyc ./libaom_total_slim.info -b ../aom_mod -o coverage.xml

Troubleshooting:

  1. Issue: gcov expect "xxx.o" while cmake produce "xxx.c.o" Fix: add CMake flag, like: cmake -DCMAKE_C_OUTPUT_EXTENSION_REPLACE=1 -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=1

Reference:

  1. https://wiki.documentfoundation.org/Development/Lcov
  2. https://qiaomuf.wordpress.com/2011/05/26/use-gcov-and-lcov-to-know-your-test-coverage/
  3. https://medium.com/@kelvin_sp/generating-code-coverage-with-qt-5-and-gcov-on-mac-os-4999857f4676
  4. https://manpages.debian.org/unstable/lcov/lcov.1.en.html
⚠️ **GitHub.com Fallback** ⚠️