Chapter 2.3 : Couverture des tests



La compilation est un peu différente pour obtenir la couverture des tests. Il faut, en effet, dire à CMake (et à notre projet en particulier) que nous voulons annoter afin d'avoir la couverture par ligne.

Il faut donc créer un dossier build si ce n'est pas déjà fait :

mkdir build

cd build



Puis appeler CMake en mode coverage :

cmake .. -DCMAKE_BUILD_TYPE=Coverage

-- The C compiler identification is GNU 9.3.0

-- The CXX compiler identification is GNU 9.3.0

-- Check for working C compiler: /usr/bin/cc

-- Check for working C compiler: /usr/bin/cc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Detecting C compile features

-- Detecting C compile features - done

-- Check for working CXX compiler: /usr/bin/c++

-- Check for working CXX compiler: /usr/bin/c++ -- works

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Detecting CXX compile features

-- Detecting CXX compile features - done

-- SELF_TESTS_MODE = yes

-- Build for tests COVERAGE

-- Configuring done

-- Generating done

-- Build files have been written to: TestGitlabCI/build



Puis Make :

make

Scanning dependencies of target accord_shadok

[ 25%] Building CXX object src/CMakeFiles/accord_shadok.dir/accord_shadok.cpp.o

[ 50%] Linking CXX shared library libaccord_shadok.so

[ 50%] Built target accord_shadok

Scanning dependencies of target test_accord_shadok

[ 75%] Building CXX object TESTS/TEST_ACCORD_SHADOK/CMakeFiles/test_accord_shadok.dir/main.cpp.o

[100%] Linking CXX executable test_accord_shadok

[100%] Built target test_accord_shadok



Lançons les tests unitaires :

make test

Running tests...

Test project /home/pierre/projects/COURS/INTRODUCTION_GITLAB/build/Correction/Final/TestGitlabCI/build

Start 1: TestAccordShadok

1/1 Test #1: TestAccordShadok ................. Passed 0.01 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) = 0.01 sec



Maintenant, nous pouvons appeler gcovr pour avoir notre couverture de test :

gcovr -r ../ --exclude-throw-branches

------------------------------------------------------------------------------

GCC Code Coverage Report

Directory: ../

------------------------------------------------------------------------------

File Lines Exec Cover Missing

------------------------------------------------------------------------------

TESTS/TEST_ACCORD_SHADOK/main.cpp 10 10 100%

src/accord_shadok.cpp 4 4 100%

------------------------------------------------------------------------------

TOTAL 14 14 100%

------------------------------------------------------------------------------



  • L'option -r permet de donner le dossier principal de notre projet (ici le dossier parent de build)
  • L'option --exclude-throw-branches permet d'exclure les branches liées aux exceptions, comme celles de la librairie standard qui ajoutent du bruit dans l'analyse de gcovr


Comme nous pouvez le constater, gcovr est très content. Nous avons une couverture de tests de 100\%.

Nous pouvons aussi lui demander un rapport de test en xml (dans notre cas Coverage.xml, disponible ici) :

gcovr -r ../ --exclude-throw-branches --xml Coverage.xml



Le plus intéressant lorque l'on développe, c'est le rapport complet en html (d'où le --html-details) :

gcovr -r ../ --exclude-throw-branches --html cov_report.html --html-details



Le fichier principal de ce rapport est donc cov_report.html. Nous avons :