Gcovr tool that shows C code coverage and related command examples

Source: Internet
Author: User

Gcovr tool that shows C code coverage and related command examples

Recently, I accidentally found the gcovr tool online to show the code coverage rate of a unit test for a project. After using the tool, I thought it was quite good, so I wrote this article for reference by related developers.

In short, gcovr is a tool that shows code coverage in unit tests in a variety of ways (including list, XML, and HTML webpage). The latest version is 3.2. Gcovr: https://pypi.python.org/pypi/gcovr /.

This tool runs in Linux. After decompression, copy the gcovr In the scripts directory to the/usr/bin directory.

The following uses the FindStackDirection. c program to find the growth direction of the stack as an example to describe how to use gcovr.

The program source code is:

/ ************************************************* *********************
* Copyright (C) 2015, Zhou Zhaoxiong.
*
* File Name: FindStackDirection.c
* Document ID: None
* Summary: Finding the growth direction of the stack
* Other instructions: None
* Current version: V1.0
* Author: Zhou Zhaoxiong
* Completion date: 20151218
*
********************************************** ******************** /
#include

// redefine the 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 Modified by Modified content
* ------------------------------------------------- --------------
* 20151218 V1.0 created by Zhou Zhaoxiong
********************************************** ********************* /
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 Modified by Modified content
* ------------------------------------------------- --------------
* 20151218 V1.0 created by Zhou Zhaoxiong
********************************************** ********************* /
void FindStackDirection (void)
{
    UINT8 iStackAddr = 0; // used to get the stack address
    static UINT8 * pStackAddr = NULL; // used to store the address of the first iStackAddr

    if (pStackAddr == NULL) // first entry
    {
        pStackAddr = & iStackAddr; // Save the address of iStackAddr
        FindStackDirection (); // recursion
    }
    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 downward.
        {
            printf ("Stack grows down! \ n");
        }
        else
        {
            printf ("Bad stack! \ n");
        }
    }
}

1. Program compilation and execution
Upload the sample program to a Linux machine and use "gcc-fprofile-arcs-ftest-coverage-fPIC-O0 FindStackDirection" in the directory where the program is located. the c-o FindStackDirection command is used to compile the program and generate FindStackDirection. gcno and FindStackDirection files.

The "-fprofile-arcs-ftest-coverage-fPIC" in the compilation command is used to generate files that can be processed by the gcovr command.

Then run the "./FindStackDirection" command and generate the FindStackDirection. gcda file.

2. code coverage in the form of output list
Run the "gcovr-r." command in the directory where the program is located. The output result is as follows:

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

This is the code coverage report. We can see that after the program runs, it overwrites 85% of the Code.

3. Output branch coverage rate
Run the "gcovr-r.-branches" command in the directory where the program is located. The output result is as follows:

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

We can see that the program has 6 branches and 4 of them are executed.

4. Output XML files showing coverage
Run the "gcovr-r.-xml-pretty" command in the directory where the program is located. The output result is as follows:

.

We can see that the command execution result is the code coverage report in XML format.

5. Output HTML files with coverage
Run the "gcovr-r.-html-o findstackdire.html command in the directory where the program is located. The findstackdire.html file is generated in the current directory. Open it in a browser, as shown in:

This HTML file visually shows the code coverage rate.

You can also add the "-html-details" option to generate an independent webpage for each file in the Code project. For example, we run the "gcovr-r.-html-details-o records file in the directory where the program is located. Open the findstackdirection2.html file with a browser, as shown in:

As you can see, the "FindStackDirection. c" text on the page has an underline. Click the text to display a new page, as shown in:
Html

Finally, gcovr is an open-source software written in Python, which is only several dozen KB in size, but is powerful. Let's pay tribute to the elders who developed the software!


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.