Using Gcov and Lcov
Using Gcov and Lcov
Using 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.
menu.cppmenu.outmenu.gcnodata //youcanseethenewfilemenu.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.
1 $export GCOV_PREFIX="/home/subho/work/lab/zzz/data"
1 $export GCOV_PREFIX_STRIP=6
now lets simply run the code.
1 $./menu.out
1
2 MENU
3 1: Add
4 2: Subtract
5 3: Multiply
4: Divide
6 5: Exit
7 Enter your choice :2
8 Enter two numbers: 3 4
9 Difference -1
10 MENU
1: Add
11 2: Subtract
12 3: Multiply
13 4: Divide
14 5: Exit
Enter your choice :5
15
16
Now we can see a menu.gcda file in data folder. Copy the .gcno file
generated earlier to the data folder.
1 $cd data
2
3 $ls
4
5 menu.gcda
6
7 $cp ../menu.gcno .
8
$ls
9
10
menu.gcda menu.gcno
11
Now that we have all the necessary files lets use lcov to read the coverage
output file generated by gcov.
ex_test.infomenu.gcdamenu.gcnores
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.
1 $cd res
2 $python -m "SimpleHTTPServer" //to start a web-server or
3 $firefox index.html //to open the index.html directly using firefox brows
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.