We all know, testing is an important part of a project. But how efficient are your tests? How much of your codes have you tested? Here comes the role of code coverage tools. I recently got to work on a C++ project, and a code coverage tool (gcov and lcov) .
In this post i have taken a sample C++ program and will be generating the code coverage stats for the same using gcov and lcov. Here is my sample C++ program link. Its pretty simple menu driven program that does simple mathematical operations like addition, subtraction, multiplication and division depending on users choice.
In this demo i am not writing actual test-cases for the code but you can see the changes in the coverage graphs depending upon your choice.
To start with we need to install gcov. Gcov comes with gcc compiler. So if you have gcc installed then gcov will work for you. Next you need to have lcov. I am working on Fedora 21, so for me its a yum install.
$yum install lcov
Next lets start with compiling our code. Here my source file name is menu.cpp
$g++ -o menu.out --coverage menu.cpp
The –coverage option here is used to compile and link code needed for coverage analysis. You will find a menu.gcno file in the folder. Next we need to export two variables namely GCOV_PREFIX and GCOV_PREFIX_STRIP. Set GCOV_PREFIX to the folder you want the output files to be in.
$ls menu.cpp menu.out menu.gcno data // you can see the new file menu.gcno
For me , the project is in “/home/subho/work/lab/zzz/” and inside this i have created a folder named data where i want the data files or .gcda file to be generated. so i set my GCOV_PREFIX to “/home/subho/work/lab/zzz/data” and the GCOV_PREFIX_STRIP equal to the the number of forward slashes or “/” in the path.
$export GCOV_PREFIX="/home/subho/work/lab/zzz/data"
$export GCOV_PREFIX_STRIP=6
now lets simply run the code.
$./menu.out
MENU 1: Add 2: Subtract 3: Multiply 4: Divide 5: Exit Enter your choice :2 Enter two numbers: 3 4 Difference -1 MENU 1: Add 2: Subtract 3: Multiply 4: Divide 5: Exit Enter your choice :5
Now we can see a menu.gcda file in data folder. Copy the .gcno file generated earlier to the data folder.
$cd data $ls menu.gcda $cp ../menu.gcno . $ls menu.gcda menu.gcno
Now that we have all the necessary files lets use lcov to read the coverage output file generated by gcov.
$lcov -t "result" -o ex_test.info -c -d .
Here ex_test.info is my output file.
-t sets a test name
-o to specify the output file
-c to capture the coverage data
-d to specify the directory where the data files needs to be searched
Now we will generate out html output for the statistics.
$genhtml -o res ex_test.info
-o To specify the output folder name.
Now on doing ls, you can see a folder named “res“.
$ls ex_test.info menu.gcda menu.gcno res
Now its time to enjoy the fruits of your labor 😛 . Go into the res folder and start a server or you can simply open the index.html file in your web-browser.
$cd res $python -m "SimpleHTTPServer" //to start a web-server or $firefox index.html //to open the index.html directly using firefox browser
Now we can click on the links to check the code coverage stats. The Red lines are the ones not executed or uncovered region. The blue lines are the ones covered. Also you can look at the Line data section for the number of times the lines have been executed.
You can look at these files in GitHub.
Hi, thank you for your tutorial, i do it like you, but i get the error and i don’t know how to fix it.
sonnguyen@ubuntu:~/workspace/test_gcov/data$ genhtml -o res ex_test.inforReading data file ex_test.infor
genhtml: ERROR: cannot read file ex_test.infor!
Can you help me fix it?
Thank you very much!
In `sonnguyen@ubuntu:~/workspace/test_gcov/data$ genhtml -o res ex_test.infor`
the file extension is wrong. it should be “.info”
Excelent guide 🙂 It was very useful to me. Thanks
Glad that it helped , cheers! 🙂
Thanks!
I am getting error in while installing lcov…
> $ yum install lcov
> Loaded plugins: fastestmirror
> Loading mirror speeds from cached hostfile
> * base: centos.mirror.constant.com
> * extras: mirrors.rit.edu
> * updates: mirror.beyondhosting.net
> No package lcov available.
> Error: Nothing to do
Can anyone plz help me what is wrong here
Working with cent OS minimal version..
You can try downloading the rpm from [1] and installing.
[1] https://rpmfind.net/linux/rpm2html/search.php?query=lcov
Hello, I want to know if this could be used for test cases written using C language and contains some dependency with external libraries.
It is actually a great and useful piece of information. I’m
satisfied that you simply shared this helpful info with us.
Please keep us informed like this. Thank you for
sharing.
It works like a charm. It has some complications with gtest . When I run the data files, it complaints that it could not find the google test headers files. What is wrong? Any idea?