C/C ++ code coverage tool gcov and lcov getting started

Source: Internet
Author: User
Tags gcov
C/C ++ code coverage tool gcov and lcov getting started

 

Http://magustest.com/blog/whiteboxtesting/using-gcov-lcov/

 

IX. 12,201 0
In
White box testing
, Software testing

Gcov
IsCodeThe coverage tool is GCC.
Built-in tools. The following describes how to use gcov
To collect code overwrite information.
To use gcov
To collect code overwrite information, you need
Add the two options "-fprofile-arcs-ftest-coverage" to compile the code.ProgramCompile

Gcc-fprofile-arcs-ftest-coverage hello. C-O hello

After compilation, an executable file hello and hello will be generated. gcno file. This is generated if the "-ftest-coverage" parameter is included when the GCC is used to compile the file. gcno file, which contains information such as program blocks and row numbers.
Next, you can run the hello program.

./Hello 5
./Hello 12

After running, a hello. gcda file is generated. If an executable file is compiled with the "-fprofile-arcs" parameter and runs at least once, it is generated. This file contains the basic program block jump information. Next, you can use gcov to generate code overwrite information:

Gcov hello. c

After running, two files hello. C. gcov and myfunc. C. gcov are generated. Open the following information:

-: 0: Source: myfunc. c
-: 0: Graph: Hello. gcno
-: 0: Data: Hello. gcda
-: 0: runs: 1
-: 0: Programs: 1
-: 1: # include
-: 2:
-: 3: void test (INT count)
1: 4 :{
-: 5: int I;
10: 6: for (I = 1; I <count; I ++)
-: 7 :{
9: 8: If (I % 3 = 0)
3: 9: printf ("% d is divisible by 3/N", I );
9: 10: If (I % 11 = 0)
#####: 11: printf ("% d is divisible by 11/N", I );
9: 12: If (I % 13 = 0)
#####: 13: printf ("% d is divisible by 13/N", I );
-: 14 :}
1: 15 :}

The code lines marked as ##### are not executed, and the information covered by the Code is correct. However, it is a cup of cake for people to read the text. Don't worry. There is another tool called lcov.
, You can use a program to parse these obscure characters and finally output a report in HTML format. Okay!

Lcov-D.-T 'Hello test'-O 'Hello _ test.info '-B.-C

Specify lcov in the current directory ". "find the code overwrite information and output it as 'Hello _ test.info '. This hello_test.info is an intermediate result. You need to use genhtml to process it. genhtml is a tool in lcov.

Genhtml-o Result hello_test.info

The output directory is result. A complete HTML report is generated, and a connection is established to connect the directory to the directory of any web server. You can view the report.

 

Test result Overview


Coverage of a specific file

Gcov and lcov can basically meet the need to collect code coverage information during the test, but unfortunately gcov cannot collect code coverage information for the. So file.

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.