Linux code coverage Detection

Source: Internet
Author: User

The GCC/g ++ comes with the gcov tool to automatically generate an executionCodeGcda. You only need to compile the macro-fprofile-arcs-ftest-coverage to generate the gcno file.

Specific information is visible

Http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Gcov-Intro.html#Gcov-Intro

This section describes how to use the file generated by gcov to generate a more intuitive web page tool lcov.Http://ltp.sourceforge.net/coverage/lcov.phpDownload this tool. Let's take a simple example to see how to use this tool.

1. Install lcov

# Tar-zxvf lcov-1.10.tar.gz

# Make install

In this way, we can have two tools, lcov genhtml, which can be used to directly run the command to see the help information-help you understand ..

 

2. Test the CPP File
 
# Include "stdio. H "# include" string. H "int test (int I) {if (I> 0) {printf (" IU> 0 \ n ");} else {printf ("I <0 \ n") ;}return 0 ;}int main () {test (1); Return 0 ;}

A file named test. gcno is generated.


3. RunProgram

./OK

Generate a gcda File

 

4. Execute lcov to generate a file

Lcov-D.-t OK-O coverage.info-B.-C

-D directory with execution Program (OK) after-O indicates file generation

Since then, the conditions for generating web pages have been fully met.


5. Generate a webpage

Genhtml-o Result coverage.info

-O path + conversion information

The page information is as follows: You can clearly see which branches of the current Code have not been reached.


Summary:

Code coverage helps us perform unit tests to a certain extent. We can perform the above tests on some test cases to see if the test cases can well cover all the branches of the Code, further improve code Stability.

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.