题目
针对下列程序段[1],需要()个测试用例[2]才可以满足语句覆盖的要求。 switch(value) case 0: other=30; break; case 1: other=50; break; case 2: other=300; case 3: other=other/value; break; default: other=other * value; A.2 B.3 C.4 D.5
针对下列程序段[1],需要()个测试用例[2]才可以满足语句覆盖的要求。 switch(value) case 0: other=30; break; case 1: other=50; break; case 2: other=300; case 3: other=other/value; break; default: other=other * value;
A.2
B.3
C.4
D.5
A.2
B.3
C.4
D.5
题目解答
答案
C
解析
语句覆盖要求每个可执行语句至少被执行一次。本题需分析switch结构中各分支的执行路径,确定最少测试用例数量。关键点在于:
case 2无break,会执行后续case 3的语句;default分支在所有case不匹配时执行;- 每个语句(含不同
break)需被覆盖。
分析各分支语句
case 0:other=30; break;
需value=0触发。case 1:other=50; break;
需value=1触发。case 2:other=300;(无break)
执行后会进入case 3,需value=2触发。case 3:other=other/value; break;
可通过value=2(连带case 2)或value=3单独触发。default:other=other * value;
需value不等于0,1,2,3(如value=4)触发。
最小测试用例设计
value=0:覆盖case 0的两条语句。value=1:覆盖case 1的两条语句。value=2:覆盖case 2的other=300;和case 3的两条语句。value=4:覆盖default的语句。
总计4个测试用例,满足所有语句覆盖要求。