C code coverage Tool

Source: Internet
Author: User
Tags gcov

From: http://qa.taobao.com /? P = 7218

C/C ++ has very few statistical tools for code coverage. Compared with Java, there are very few open-source and free tools. It is very rare to use and open-source tools. Finally, we chose gcov-based lcov as the coverage rate statistics Tool for nginx testing. The reason for selecting lcov is simple: first, it is suitable for gcov as a test coverage tool supporting GCC; second, nginx is a pure C program, and gcov shows the coverage rate of pure C code more accurately; third, as an extension of gcov, lcov can generate an intuitive HTML coverage report with source code. Next, let's take a look at how to use lcov to show the nginx test code coverage.

1. Download and install

1. lcov home page: http://ltp.sourceforge.net/coverage/lcov.php

2. If you have the root permission to decompress the package, directly make Insall and install it to the execution directory of the system. Then, you can execute the lcov tool command anywhere.

3. If you do not have the root or sudo permissions, you can define the prefix variable in makefile and direct it to the installation directory with the permissions (for example: prefix =/home/mylcov), then make install to the specified directory, and use the lcov tool command in the form of a command with a path (for example: /home/mylcov/lcov .....).

4. gcov does not need to be installed and is released together with GCC and Linux.

Ii. How to calculate coverage

1. To enable lcov to finally collect statistics and display coverage, you need to add some options when compiling the tested nginx to enable gcov code coverage support. Compilation option:-fprofile-arcs-ftest-coverage link option:-lgcov nginx uses Autoconf to generate makefile. We only need to add the preceding options When configuring configure, run the following command line to enable nginx code coverage. ./Configure-with-PCRE-with-http_ssl_module-With-CC-opt = "-fprofile-arcs-ftest-coverage"-with-LD-opt =-lgcovThe highlighted red and bold parts are the aforementioned options.

2. Compile and install nginx and initialize lcov statistics. After the configure command is executed, make and make install the nginx version with coverage of statistical code. At this time, you will find many. gcno and. gcda files in the source code compiling directory. gcno is the path arc-length file for coverage statistics, and. gcda is the coverage file. The next thing we need to do is to initialize the coverage data and initialize it before every statistical coverage. Execute lcov-D in the compilation directory of the source code just now. /-Z, which indicates that the current directory (..

3. Start nginx and perform various tests.

4. After the test is completed, the collection coverage data is still executed in the nginx Source Code compiling directory. /-D. /-c-o outputfile.info indicates that the relative directory of the statistics is the current directory. The coverage data under the current directory is collected and summarized into the file named outputfile.info.

5. Use a summary data file to generate an intuitive webpage-based report with code coverage. Run genhtml outputfile.info in the directory where outputfile.info is located, the summary data file outputfile.info is used to generate the webpage format reports for some columns.

Iii. Strengths and weaknesses of lcov

Lcov performs test coverage rate statistics based on gcov. lcov is also generated for Linux kernel test coverage rate. Therefore, it provides good support for GCC compiling programs in Linux, in addition, reports in HTML form are presented to testers, and the results are intuitive and conducive to analysis. The coverage of lcov test is also comprehensive, including row coverage, branch coverage, and function coverage. However, it is also unfortunate that dynamic link library files in the form of so cannot be supported, the coverage rate of fully written C ++ code is calculated in the form of C after the C ++ code is processed by the compiler, therefore, the covered data is slightly different from the actual code Display page, but it can still be used as a reference for testing coverage. In general, lcov can meet the requirements of code coverage rate statistics for pure C static compilation programs such as nginx.

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.