<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Naveenkhasyap</title>
    <description>The latest articles on DEV Community by Naveenkhasyap (@naveenkhasyap).</description>
    <link>https://dev.to/naveenkhasyap</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F288616%2F715c8e5b-cfac-4ee1-af55-710a97d328f1.jpeg</url>
      <title>DEV Community: Naveenkhasyap</title>
      <link>https://dev.to/naveenkhasyap</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/naveenkhasyap"/>
    <language>en</language>
    <item>
      <title>Generating Code Coverage Report Using GNU Gcov &amp; Lcov.</title>
      <dc:creator>Naveenkhasyap</dc:creator>
      <pubDate>Fri, 16 Feb 2024 09:21:56 +0000</pubDate>
      <link>https://dev.to/naveenkhasyap/generating-code-coverage-report-using-gnu-gcov-lcov-59p</link>
      <guid>https://dev.to/naveenkhasyap/generating-code-coverage-report-using-gnu-gcov-lcov-59p</guid>
      <description>&lt;p&gt;Hi Readers ,&lt;/p&gt;

&lt;p&gt;Recently I was going through online contents on how to generate report for the code in C++ project . So I curated this article thinking that it might help other who are in dire need of setting up code coverage reports.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Gcov.html" rel="noopener noreferrer"&gt;GCOV — GNU COVerage&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a tool developed to help user to generate reports for the C++ code written in the project . which helps you to know percentile of test cases covered&lt;/p&gt;

&lt;p&gt;Usually project development cycle will be as&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You develop the software as per your pre-decided design.&lt;/li&gt;
&lt;li&gt;Write Unit test cases to check the functionality of the code written.&lt;/li&gt;
&lt;li&gt;Using these Test case written check the possible code coverage by your test cases.&lt;/li&gt;
&lt;li&gt;And generate report with the above and try to maximise the code coverage percentile by covering more test cases and hitting more lines of codes in your api’s.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I am not going to discuss all the 4 points above , as you might have already finalised your design and started coding accordingly with proper unit test cases , I will explain how to write unit test with a simple example code along with how to set up Gcov and other required tools for generating reports .&lt;/p&gt;

&lt;p&gt;Hands Onnnnn…&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExdzgzMGExeTlma3k1bnJzY3Q4bnIwZW5mYTNneWYydzg3aHc1NHppbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2Fl2Je3ktsieOfOGa1G%2Fgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia4.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExdzgzMGExeTlma3k1bnJzY3Q4bnIwZW5mYTNneWYydzg3aHc1NHppbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2Fl2Je3ktsieOfOGa1G%2Fgiphy.gif" alt="handson"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First we will write a simple program say for example Program which gives you the biggest number of given three number&lt;/p&gt;

&lt;p&gt;main.cpp&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include&amp;lt;iostream&amp;gt;
using namespace std;
int GreatestOfThree(int a,int b,int c){
  if((a&amp;gt;b) &amp;amp;&amp;amp; (a&amp;gt;c)){    //for a &amp;gt; b and a&amp;gt;c case
    return a;
   }
   else if(b&amp;gt;c){    //for b&amp;gt;c case 
    return b;
   }
  else{
    return c;
  }
 return 0;
}
//After checking for correctness comment main function to test //main_test.cpp
int main()
{
 // your code goes here
 int a,b,c;
 cin&amp;gt;&amp;gt;a&amp;gt;&amp;gt;b&amp;gt;&amp;gt;c;
 cout&amp;lt;&amp;lt;GreatestOfThree(a,b,c);
 return 0;
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So above is our main program for finding out greatest number in the given three numbers .&lt;/p&gt;

&lt;p&gt;now we will try to build and test whether above code works .. I am compiling my code in Linux machine so below is the command to build the main.cpp&lt;/p&gt;

&lt;p&gt;&lt;code&gt;g++ -o main main.cpp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;g++ is the compiler here , -o option to mention the output file name here I am giving it as main and the source file which I want to compile i.e. main.cpp&lt;/p&gt;

&lt;p&gt;once the above command is success without any errors in terminal&lt;/p&gt;

&lt;p&gt;just run the binary/executable as follows&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

$./main 1 2 3 
$ 3


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;this should give out put as 3 shown above if everything is correct.&lt;/p&gt;

&lt;p&gt;Next step is to write test cases , Here I am using Gtest, which is yet another open source powerful library for writing Unit test cases for C++ developed by Google.&lt;/p&gt;

&lt;h2&gt;
  
  
  Gtest Setup
&lt;/h2&gt;

&lt;p&gt;First to set up gtest in your machine you need to install gtest library ,so for this we have to get the source code first and build in our machine locally and link the libraries to standard lib path i.e. /usr/lib&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 1&lt;/em&gt; . get source from here&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 1&lt;/strong&gt; : Download with one click&lt;/p&gt;

&lt;p&gt;google/googletest&lt;br&gt;
Google Test. Contribute to google/googletest development by creating an account on GitHub.&lt;br&gt;
github.com&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Method 2&lt;/strong&gt;: Clone source from repository&lt;/p&gt;

&lt;p&gt;if git program is not installed in your machine first get that and enter below command to get source&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone https://github.com/google/googletest.git&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;_Step 2 _ : Extract the archived googletest folder in current directory&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unzip googletest-master.zip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 3&lt;/em&gt; : cd in to make folder within googletest directory[cd-change directory ]&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd googletest-master&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;_Step 4 _ :  type make , this will build the googletest library&lt;/p&gt;

&lt;p&gt;&lt;code&gt;make&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Step 5&lt;/em&gt; : copy built library in the current folder to /usr/lib because compiler checks for libs in default path which is /usr/lib&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo cp *.a /usr/lib&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now you can include gtest header and start writing test cases for your code&lt;/p&gt;

&lt;p&gt;&lt;code&gt;main_test.cpp&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

#include "gtest/gtest.h"
#include "main.cpp"
TEST(GreaterTest,AisGreater){
EXPECT_EQ(3,GreatestOfThree(3,1,2));
};
TEST(GreaterTest,BisGreater){
EXPECT_EQ(3,GreatestOfThree(1,3,2));
};
TEST(GreaterTest,CisGreater){
EXPECT_EQ(3,GreatestOfThree(1,2,3));
};
int main(int argc,char**argv)
{
testing::InitGoogleTest(&amp;amp;argc, argv);
    return RUN_ALL_TESTS();
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above is the test file to test our GreatestOfThree function, but before building this file notice one thing here we are including main function in test file also so if you compile you will get error saying multiple main function , hence to test out test cases first comment the main function in main.cpp then compile it will work.&lt;/p&gt;

&lt;p&gt;so to compile above code command is as below&lt;/p&gt;

&lt;p&gt;&lt;code&gt;g++ -o main main_test.cpp -L /usr/lib -I/usr/include&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will create an executable named main then just run that to see test cases executing in terminal&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

./main
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from GreaterTest
[ RUN      ] GreaterTest.AisGreater
[       OK ] GreaterTest.AisGreater (0 ms)
[ RUN      ] GreaterTest.BisGreater
[       OK ] GreaterTest.BisGreater (0 ms)
[ RUN      ] GreaterTest.CisGreater
[       OK ] GreaterTest.CisGreater (0 ms)
[----------] 3 tests from GreaterTest (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 3 tests.
You will get output as above if everything is correct.


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Generating Code Coverage Report&lt;/em&gt;&lt;br&gt;
Now I will show how to generate coverage report for the tests written till now&lt;/p&gt;

&lt;p&gt;So, for this task we are using tool called Gcov , Usually this will come up with GCC inbuilt so no need to install separately to use this tool one must add flags to generate reports flags to be added as follows&lt;/p&gt;

&lt;p&gt;&lt;code&gt;g++ -o main  -fprofile-arcs -ftest-coverage main_test.cpp -L /usr/lib -I/usr/include&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will generate an executable with coverage flag enabled so next you need to run the executable to generate files required for coverage report generation&lt;/p&gt;

&lt;p&gt;&lt;code&gt;./main&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will generate two types of files one is ending with extension .gcno which has info for basic block construction and line numbers this is generated because of the -ftest-coverage flag which we added while compiling with g++ command , and other type is ending with .gcda will have profiling and coverage information which is generated because of flag -fprofile-arcs in g++ command . we need both to create coverage reports .&lt;/p&gt;

&lt;p&gt;To generate report you need to pass your test file to gcov.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gcov main_test.cpp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;this will generate coverage report . which will be named as main_test.cpp.gcov since the content is not much intuitive to read by layman there is another tool which is used to generate html files of these reports which can be viewed in the browser later.&lt;/p&gt;

&lt;p&gt;So here we will use lcov tool to check line coverage and generate html reports . For this command is as shown below.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lcov --coverage --directory . --output-file main_coverage.info&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a coverage report by taking .gcno and .gcda files from ‘.’ i.e. current folder where these files are present and generate an output file with coverage info in file called main_coverage.info .&lt;/p&gt;

&lt;p&gt;Now we have coverage info lets display it in html file using below command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;genhtml main_coverage.info --output-directory out&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the above command will generate html reports in output directory named as out.&lt;/p&gt;

&lt;p&gt;once it is done you can go through the out folder and open index.html to check coverage percentile. It will display both Line coverage and Function coverage as below .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1400%2Fformat%3Awebp%2F1%2AVdt6Wz08UHq49V0sxB6LuA.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afit%3A1400%2Fformat%3Awebp%2F1%2AVdt6Wz08UHq49V0sxB6LuA.jpeg" alt="coverage"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By this you can conclude how much of your code is getting covered by test cases written by you. Since I just explained you with a simple example you can go through each library/framework documentation further to know more and modify according to your project requirements .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExZDRxOWxvcWh5eDhhNnJhYnhiaG42bzI3bjFua2poaG9pdGkyMGliYSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2FnGMnDqebzDcfm%2Fgiphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExZDRxOWxvcWh5eDhhNnJhYnhiaG42bzI3bjFua2poaG9pdGkyMGliYSZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw%2FnGMnDqebzDcfm%2Fgiphy.gif" alt="Happy Coding"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Sources&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;Gtest source : &lt;a href="https://github.com/google/googletest/tree/master/googletest" rel="noopener noreferrer"&gt;https://github.com/google/googletest/tree/master/googletest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Gcov : &lt;a href="https://gcc.gnu.org/onlinedocs/gcc/Gcov.html" rel="noopener noreferrer"&gt;https://gcc.gnu.org/onlinedocs/gcc/Gcov.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lcov : &lt;a href="https://github.com/linux-test-project/lcov" rel="noopener noreferrer"&gt;https://github.com/linux-test-project/lcov&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;gif image courtesy gify.com :)&lt;/p&gt;

&lt;p&gt;I hope you liked this article .&lt;/p&gt;

&lt;p&gt;Keep Exploring and Sharing :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Believe Strive and Achieve .&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>gcov</category>
      <category>codecoverage</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
