一、实验目的
掌握基于覆盖理论与基本路径的基本白盒测试方法和实践
二、实验要求
运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。
三、实验内容
根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。
所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。
包括的内容有:
1) 被测原代码(本应该是使用第二次修改代码,但为c语言代码无法进行junit测试):
float commission (int headphone, int shell, int protector)
public class MonyCount {
/**
* @param args
*/
//用于判断输入是否正确
static boolean charge(String headphone, String shell, String protector){
if(Integer.valueOf(headphone).intValue()<0||
Integer.valueOf(shell).intValue()<0||
Integer.valueOf(protector).intValue()<0){
System.out.println("输入数量不满足要求");
return false;
}else{
return true;
}
}
static //计算佣金的公式
float commission (String Headphone, String Shell, String Protector){
//实现字符串到数字的转化
int headphone=0;
int shell=0;
int protector=0;
try {
headphone = Integer.valueOf(Headphone).intValue();
shell= Integer.valueOf(Shell).intValue();
protector= Integer.valueOf(Protector).intValue();
} catch (NumberFormatException e) {
e.printStackTrace();
}
int total=0;
float money=0;
total=80*headphone+10*shell+8*protector;
if(total<1000){
money=(float) (total*0.1);
}
if(total>=1000&&total<1800){
oney=(float) ((total-1000)*0.15+100);
}
if(total>=1800){
money=(float) (220+(total-1800)*0.2);
}
return money;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String headphone;
String shell;
String protector;
float count;//用于输出计算后得到的佣金
//分别对输入的数值进行校验
while(true){
System.out.println("请分别输入三种手机配件的销售情况:");
headphone=sc.next();
shell=sc.next();
protector=sc.next();
//改函数用于判断输入是否符合规范
if(!charge(headphone,shell,protector)){
continue;
}
count=commission(headphone,shell,protector);
System.out.println("应支付的佣金为:"+count);
}
}
}
2)依据覆盖技术,测试用例列表:
commission函数DD-路径图:
语句覆盖、路径覆盖:
A-B-D-G
A-B-C-E-G
A-B-C-F-G
用例设计:
编号 | 输入 | 预期输出 | 实际输出 | 执行路径 | 是否通过 |
1 | 0 0 0 | 0 | 0 | ABDG | √ |
2 | 15 10 10 | 157.0 | 157.0 | ABCEG | √ |
3 | 30 20 10 | 396.0 | 396.0 | ABCFG | √ |
判定条件覆盖、组合覆盖:
编号 | B< 1000 | C >= 1000 | C <= 1800 | 路径 |
1 | T | F | F | BDG |
2 | F | T | T | BCEG |
3 | F | T | F | BCFG |
用例设计
编号 | 输入 | 预期输出 | 实际输出 | 执行路径 | 是否通过 | 覆盖条件 |
1 | 1 1 1 | 9.8 | 9.8 | ABDG | √ | 1 |
2 | 15 15 15 | 170.5 | 170.5 | ABCEG | √ | 2 |
3 | 20 20 20 | 252.0 | 252.0 | ABCFG | √ | 3 |
junit测试脚本以及结果:
package test1;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class MonyCountTest {
private static MonyCount monyCount= new MonyCount();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@SuppressWarnings("static-access")
@Test
public void testCommission() {
assertEquals(0.0,monyCount.commission("0", "0", "0"),0.01);
assertEquals(157.0,monyCount.commission("15", "10", "10"),0.01);
assertEquals(396.0,monyCount.commission("30", "20", "10"),0.01);
assertEquals(9.8,monyCount.commission("1", "1", "1"),0.01);
assertEquals(170.5,monyCount.commission("15", "15", "15"),0.01);
assertEquals(252.0,monyCount.commission("20", "20", "20"),0.01);
}
}
结果:
测试参数化脚本:
package test1;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParameterizedTest{
MonyCount com;
private String headphone;
private String shell;
private String protector;
private double result;
@SuppressWarnings("rawtypes")
@Parameters
public static Collection data(){
return Arrays.asList(
new Object[][]{
{"0", "0", "0", 0},
{"15", "10", "10", 157.0},
{"30", "20", "10", 396.0},
{"1", "1", "1", 9.8},
{"15", "15", "15", 170.5},
{"20", "20", "20", 252.0},
}
);
}
public ParameterizedTest(String headphone,String shell,String protector,double result){
com=new MonyCount();
this.headphone=headphone;
this.shell=shell;
this.protector=protector;
this.result=result;
}
@SuppressWarnings("static-access")
@Test
public void testCommission() {
assertEquals(result,com.commission(headphone, shell, protector), 0.00001);
}
}
结果:
打包测试脚本:
package test1;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({MonyCount.class, ParameterizedTest.class})
public class AllTest {
}
但是结果却出乎意料:
4、测试小结:
测试找到的缺陷清单:
首先如果输入的参数带有非数字会导致崩溃(不过此次测试没有这方面测试)
对源代码的修改建议:
建议先对输入参数的合法性进行判断,最后打包测试会产生错误也说明了部分函数存在错误
测试总结与心得体会:
首先是测试的代码质量高低对测试人员的测试进程会产生巨大的影响,其次是感觉上面所说的部分函数不能运行,却没有标明是哪个函数出错,只能已知monycounttest所测的其中一个函数是无误的。
另外就是要是合法性判断函数和计算函数不在一个函数内来进行分别测试是不是会对测试的结果产生不良的影响。