Introduction to the GCOVR tool showing C code coverage and examples of related command usage

Source: Internet
Author: User

(I am participating in the 2015 blog Star Selection, invite you to vote, thank you: HTTP://VOTE.BLOG.CSDN.NET/BLOGSTAR2015/CANDIDATE?USERNAME=ZHOUZXI)

Recently, I accidentally found the GCOVR tool on the Web because I wanted to show code coverage for unit tests for a project. After use, think this tool is quite good, so write this article, for the relevant developers to reference.

In short, GCOVR is a tool that displays code coverage in unit tests in a variety of ways, including list mode, XML file mode, HTML Web page style, and so on, and the latest version is 3.2. The GCOVR is: https://pypi.python.org/pypi/gcovr/.

The tool runs under Linux and, after decompression, copies the GCOVR in the scripts directory to the/usr/bin directory.

The following is an example of a FINDSTACKDIRECTION.C program that looks for the growth direction of the stack, and describes how GCOVR is used.

The program source code is:

/*********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: FINDSTACKDIRECTION.C* File ID: None* Content Summary: Looking for the growth direction of the stack* Other instructions: None* Current version: V1.0* Author: Zhou Zhaoxiong* Completion Date: 20151218***********************************************************************/#include <stdio.h>Redefine data type typedef unsigned char uint8;typedef signed int int32;//function declaration void findstackdirection (void);*********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151218 V1.0 Zhou Zhaoxiong created***********************************************************************/int32 Main () {findstackdirection ();return 0;}/*********************************************************************** Function Description: Find stack growth direction* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* ---------------------------------------------------------------* 20151218 V1.0 Zhou Zhaoxiong created***********************************************************************/void findstackdirection (void) {UINT8 istackaddr = 0; Used to get the stack addressstatic UINT8 *pstackaddr = NULL; Address for storing the first istackaddrif (pstackaddr = = NULL)//First entry    {                          pstackaddr = &iStackAddr; Save Istackaddr's addressfindstackdirection (); Recursive    }Else//second entry    {  if (&istackaddr > Pstackaddr)//The address of the second istackdirection is greater than the first istackdirection, then the stack growth direction is upward        {   printf ("Stack grows up!\n");        }else if (&istackaddr < PSTACKADDR)//The address of the second istackdirection is less than the first istackdirection, then the stack growth direction is down        {  printf ("Stack grows down!\n");        }Else        {printf ("Bad stack!\n");        }    }}

1. Program Compilation and execution
Upload the sample program to the Linux machine and use the Gcc-fprofile-arcs-ftest-coverage-fpic-o0 findstackdirection.c-o in the directory where the program is located findstackdirection command to compile the program and generate the Findstackdirection.gcno and findstackdirection files.

The "-fprofile-arcs-ftest-coverage-fpic" in the Compile command is used to produce files that can be processed by the GCOVR command.

The "./findstackdirection" command is then executed, and the FINDSTACKDIRECTION.GCDA file is generated.

2. Code coverage in the form of output lists
Run "gcovr-r." In the directory where the program resides. command, you can see the output as follows:

------------------------------------------------------------------------------                  GCC Code Coverage ReportDirectory: .------------------------------------------------------------------------------File                   Lines    Exec  Cover   Missing------------------------------------------------------------------------------FindStackDirection.c      14      12    85%   65,73------------------------------------------------------------------------------TOTAL                     14      12    85%------------------------------------------------------------------------------

This is the code coverage report, and we can see that after the program runs, it covers 85% of the code.

3. Output Branch Coverage
Run "Gcovr-r" in the directory where the program is located. –branches "command, you can see the output as follows:

------------------------------------------------------------------------------                 GCC Code Coverage ReportDirectory: .------------------------------------------------------------------------------File                   Branches   Taken  Cover   Missing------------------------------------------------------------------------------FindStackDirection.c        6        4    66%   63,67------------------------------------------------------------------------------TOTAL                       6        4    66%------------------------------------------------------------------------------

As we can see, there are 6 branches in the program, and 4 of them are executed.

4. Output an XML file that shows coverage
Run "Gcovr-r" in the directory where the program is located. –xml-pretty "command, you can see the output as follows:

<?xml version= "1.0"?><! DOCTYPE coverage SYSTEM ' HTTP://COBERTURA.SOURCEFORGE.NET/XML/COVERAGE-03.DTD ' ><coverage branch-rate="0.666666666667" line-rate="0.857142857143"  Timestamp="1450424892" version="GCOVR 3.2"> <sources>  <source>.</Source> </sources> <packages>  < package branch-rate="0.666666666667" complexity="0.0"  Line-rate="0.857142857143" name="">      <classes>    <class branch-rate="0.666666666667" complexity= "0.0"  FileName="findstackdirection.c" line-rate="0.857142857143"name=  "Findstackdirection_c">               <methods/>     <lines>      < line Branch="false" hits= "0" number ="+"/ >      < Line Branch="false" hits="1" number =  "/>"      < Line Branch="false" hits="1" number = "/>"      < line Branch="false" hits="1" number ="PNS"/ >      < Line Branch="false" hits="0" number =  "/>"      < Line Branch="false" hits="2" number = "/>"      < line Branch="true" condition-coverage="50% " hits ="1" number ="> "       <conditions>        <condition Coverage="50%" number ="0" type ="Jump"/>       </conditions>      </line >      < line Branch="true" condition-coverage="50% " hits ="1" number ="> "       <conditions>        <condition Coverage="50%" number ="0" type ="Jump"/>       </conditions>      </line >      < Line Branch="false" hits="2" number ="52" />      < Line Branch="false" hits="2" number = "/>"      < line Branch="true" condition-coverage="100% (2/2)" Hits ="2"number =">        "       <conditions>        <condition Coverage="100%" number ="0" type ="Jump"/>       </conditions>      </line >      < line Branch="false" hits="1" number ="+"/ >      < Line Branch="false" hits="1" number = "/>"      < line Branch="false" hits="1" number =" >     </lines>    </class>   </Classes>  </Package > </Packages></Coverage>

As we can see, the result of the command execution is the output of the Code coverage report in XML format.

5. Output an HTML file showing coverage
Run "Gcovr-r" in the directory where the program is located. –html-o findstackdirection.html "command, you can see that the findstackdirection.html file is generated under the current directory. Open with the browser, as shown in the following:

This HTML file visually shows the coverage of the code in a graphical manner.

We can also add the "–html-details" option to generate a separate Web page for each file in the code project. For example, we run "Gcovr-r" in the directory where the program is located. –html–html-details-o findstackdirection2.html "command, You can see that the findstackdirection2.html file and the findstackdirection2.findstackdirection.c.html file are generated in the current directory. Open the findstackdirection2.html file with the browser, as shown in the following:

As you can see, the "findstackdirection.c" text on the page has an underscore, click the text, and a new page appears with the contents as shown:

As you can see, some lines of code are preceded by numbers that indicate how many times the row was run. If the "if (pstackaddr = = NULL)" Line runs 2 times, "pstackaddr = &iStackAddr;" Row has been run 1 times.

6. More GCOVR Commands
The GCOVR command, which is often used, is shown in the previous section, with more detailed GCOVR commands and instructions for the command, please refer to this document: Http://gcovr.com/guide.pdf.

7. Summary
The GCOVR tool is a pretty good choice if you want to see the code coverage you've written for C code during execution.

Here are two URLs that you can use to get a deeper understanding of GCOVR:

(1) GCOVR official website: http://gcovr.com/

(2) How to use GCOVR to generate code Coverage trend chart: http://www.cnblogs.com/jackyim/p/3772306.html

Finally, a few more words, GCOVR is a python-written open source software, the size of only dozens of KB, but powerful. Let's pay tribute to the seniors who developed the software!

Introduction to the GCOVR tool showing C code coverage and examples of related command usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.