- 论坛徽章:
- 0
|
本文主要结合gdb和gcov开源工具,在linux环境下进行白盒测试。
白盒测试的主要目的有两点,一点是验证软件需要是否在代码中被覆盖,二是进行代码覆盖率分析,尽可能多的覆盖所有软件代码。显然,最重要的第一点。分析代码覆盖率就是为了检查代码跟需求是否匹配,然后再继续根据需求添加新代码,或者删除掉根本没有执行的死代码。
所以当代码覆盖率达到100%,说明代码已经很完美的匹配了软件需求,白盒测试工作也就结束了。这就是白盒测试的根本目的。
下面就结合具体的例子说明linux白盒测试流程。
这里有个简单的 程序,计算1+2+3..+8的和 sum。如果有条程序需求,当sum不等于36,设置quit=0,否侧,设置quit=1。
$cat calnumber.c
1 #include
2
3 int main()
4 {
5 int i,sum,number;
6 int quit;
7 number = 8;
8 sum = 0;
9 for(i = 0; i calnumber.res
下面是gdb脚本的输出:
$cat calnumber.res
GNU gdb 6.5.50.20060706-cvs (cygwin-special)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-cygwin".
---------------------
Test Case 1
Breakpoint 1 at 0x401083: file calnumber.c, line 4.
Breakpoint 1, main () at calnumber.c:4
4 {
Breakpoint 2 at 0x401096: file calnumber.c, line 9.
Breakpoint 2, main () at calnumber.c:9
9 for(i = 0; i
-: 2:
-: 3:int main()
function main called 2 returned 100% blocks executed 100%
2: 4:{
2: 5: int i,sum,number;
2: 6: int quit;
2: 7: number = 8;
2: 8: sum = 0;
16: 9: for(i = 0; i $2
echo -e "\tTest Results Summary\n" >> $2
echo -e -n "Total Number of Comparisons\t\t:\t" >> $2
comparison_nums=`grep -r $output_key_word $1 | cut -f2 | wc -l`
echo -e "$comparison_nums" >> $2
echo -e -n "Total Number of Comparisons Failed\t:\t" >> $2
failed_nums=`grep -r $output_key_word $1 | cut -f2 | grep -r '= 0'| wc -l`
echo -e "$failed_nums" >> $2
echo -e -n "Total Number of Comparisons Passed\t:\t" >> $2
passed_nums=`expr $comparison_nums - $failed_nums`
echo -e "$passed_nums" >> $2
echo -e -n "Total Number of Test Cases Included\t:\t" >> $2
case_nums=`grep -r $case_key_word $1 | wc -l`
echo -e "$case_nums" >> $2
echo >> $2
echo -e -n "Percentage of Comparisons Passed\t:\t">>$2
percentage=`awk 'BEGIN{print '$passed_nums'/'$comparison_nums'}'`
echo -e "$percentage" >> $2
echo >> $2
echo --------------------------------------- >> $2
}
[ $# -ne 1 ] && usage
# Clean
rm -f *.gcno *.gcda *.c.gcov
rm -f *.ver *.rpt *.res
# Complier
gcc -fprofile-arcs -ftest-coverage -g -o $1 $1.c
# Debug
[ -e $1.gdb ] && gdb -x $1.gdb > $1.res
# Test Result Report
generate_ver $1.res $1.ver
[ -e $1.ver ] && echo "Generate VER file successfully!"
# Code Coverage Report
gcov $1.c > $1.rpt
[ -e $1.c.gcov ] && cat $1.c.gcov >> $1.rpt
[ -e $1.rpt ] && echo "Generate RPT file successfully!"
如果把calnumber.gdb中第二个用例删掉,我们再来看看结果。
$./flow.sh calnumber
Generate VER file successfully!
Generate RPT file successfully!
$cat calnumber.ver
---------------------------------------
Test Results Summary
Total Number of Comparisons : 5
Total Number of Comparisons Failed : 0
Total Number of Comparisons Passed : 5
Total Number of Test Cases Included : 1
Percentage of Comparisons Passed : 1
---------------------------------------
$cat calnumber.rpt
File `calnumber.c'
Lines executed:85.71% of 14
calnumber.c:creating `calnumber.c.gcov'
-: 0:Source:calnumber.c
-: 0:Graph:calnumber.gcno
-: 0:Data:calnumber.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include
-: 2:
-: 3:int main()
function main called 1 returned 100% blocks executed 82%
1: 4:{
1: 5: int i,sum,number;
1: 6: int quit;
1: 7: number = 8;
1: 8: sum = 0;
6: 9: for(i = 0; i
![]()
文件:
whitebox_test.zip
大小:
1KB
下载:
下载
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/68938/showart_706802.html |
|