题目
针对下列程序段[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个测试用例,满足所有语句覆盖要求。