The unit test coverage rate means that each line of code should be covered by unit tests. Therefore, it is necessary to write a unit test for each method. In addition, if there are many if statement branches in this method, it should also be covered in unit test.
In the world of C, we can use the gcov tool to calculate the coverage rate. It moves every unit test and then counts the execution of each line of code. gcov was originally used to test the coverage rate of the Linux kernel, is a command line tool. Lcov can be used, which adds GUI support based on gcov.
1 install lcov
# Wgethttp: // downloads.sourceforge.net/ltp/lcov-1.9.tar.gz
# Tar-zxvf lcov-1.9.tar.gz
# Cd lcov-1.9
# Make install
2. Run an example step by step.
The code for test. C is as follows:
# Include <stdio. h>
# Include <stdlib. h>
Int main (INT argc, char * argv []) {
Int I = 0;
Printf ("begin test... \ n ");
If (1 = argc ){
Printf ("argc = 1 \ n ");
} Else {
Printf ("argc! = 1 \ n ");
For (I = 0; I <argc; I ++)
Printf ("% d \ TOF \ t % d \ n", I + 1, argc );
}
Printf ("End test! \ N ");
}
Step 1: add-Fprofile-arcs-ftest-coverage
Parameters:
GCC test. c-Fprofile-arcs-ftest-coverage-O test
Step 2: run the test case:
[Hua @ Zhanghua test] $./Test 1 2 3 4
Begin test...
Argc! = 1
1 of 5
2 of 5
3 of 5
4 of 5
5 of 5
End test!
Step 3: statistical data has been generated in the previous step, but it is not easy to see.LcovCollect statistical data for graphical display.
Lcov -- capture -- directory.-- Output-file test.info-- Test-name Test
Lcov-zerocounters (calculator cleared)
Step 4: generateHtmlWebpage:
GenhtmlTest.info-- Output-directory output -- title "asimple test" -- show-details -- legend
3. Examples of lcov running
The built-in example is located in the $ lcov/example directory, as follows:
[Hua @ Zhanghua example] $ ll/bak/Xue/unittest/lcov-1.9/example/
Total 28
-RW ------- 1 Hua 316 Aug 6 2010 descriptions.txt
-RW ------- 1 Hua 1439 Aug 6 2010 example. c
-RW ------- 1 Hua 108 Aug 6 2010 gauss. h
-RW ------- 1 Hua 118 Aug 6 2010 iterate. h
-RW ------- 1 Hua 2482 Aug 6 2010 makefile
Drwx ------ 2 Hua 4096 Aug 6 2010 Methods
-RW ------- 1 Hua 156 Aug 6 2010 readme
To view the README file, run the following command:
Make output
4And
MakefileIntegration
To be added in the compilation stage2Compilation options, as shown below
L add-fprofile-arcs-ftest-coverage or -- coverage during compilation;
L add-ftest-coverage or-lgcov when linking;
L we recommend that you enable the-G3 option and remove the-O2 or above-level code optimization options;
Note that the use of the preceding options cannot affect the normal compilation process. Otherwise, the program running efficiency will be greatly affected. We recommend that you use the makefile parameter or the scons compilation parameter to compile,
Ifeq ($ (coverage), yes)
Cxxflags + =-fprofile-arcs-ftest-coverage
Linkercxx + =-fprofile-arcs-ftest-coverage
Opt_flags =-G
Endif
In this way, you can use makecoverage = Yes
Check to introduce these compilation options without affecting normal compilation.
if ENABLE_COV
cov-reset:
@rm -fr coverage
@find . -name "*.gcda" -exec rm {} \;
@lcov --directory . --zerocounters
cov-report:
@mkdir -p coverage
@lcov --compat-libtool --directory . --capture --output-file coverage/app.info
@genhtml -o coverage/ coverage/app.info
cov:
@make cov-report
clean-local:
@make cov-reset
check:
@make cov
endif
(The makefile can be added to cflags and ldflags.) If undefinedreference is displayed during connection
To '_ gcov_init' error, add-lgocv. After compilation, run the compiled program and exit the program.
An example of glib integration with gcov:
Http://code.google.com/p/google-highly-open-participation-gnome/issues/detail? Id = 37
5 write unit tests with googletest
Googletest (gtest)
Google open-source C ++ unit testing framework googletest series (gtest) (total)
Http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html