首页 > 代码库 > 实验二+063+陈彧

实验二+063+陈彧

一、实验目的

掌握基于覆盖理论与基本路径的基本白盒测试方法和实践

二、实验要求

运用逻辑覆盖测试的覆盖准则设计被测程序的测试用例,并运行测试用例检查程序的正确与否,给出程序缺陷小结。

三、实验内容

根据各位同学自己的被测程序,分别作出各类白盒测试技术的用例设计和相应的Junit脚本。

所有的覆盖的技术:语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖,基本路径测试方法。

1) 被测原代码

package Test1;
import java.io.IOException;
import java.util.Scanner;

public class yongjin01 {
    public static void main(String[] args){
        yongjin01 yj = new yongjin01();
        
        System.out.println("请分别输入三种手机配件的销售情况:");
        
            double headphone = yj.Input(0);
        
            double shell = yj.Input(0);
            double protector = yj.Input(0);
            System.out.println("耳机数量:"+headphone+"\n手机壳数量:"+shell+" \n手机贴膜数量:"+protector);
            double commission = yj.Commission(headphone, shell, protector);
            System.out.println("销售佣金:"+commission);
        
        }
    public double Input(double number){
        double num = 0;
        Scanner scanner = new Scanner(System.in);
     try{
        if(num<0){
            System.out.println("输入的数量不满足要求!");
            
        }else{
            num = scanner.nextDouble();
            return num;
        }
     }catch(Exception e){System.out.println("输入不合法!");}
        return num;
                
    }
    public double Commission(double headphone,double shell,double protector){
        double commission = 0;   //定义佣金;
        double headphonePrice = 80;
        double shellPrice = 10;
        double protectorPrice = 8;
        double headphonesales = headphonePrice*headphone;
        double shellsales = shellPrice*shell;
        double protectorsales = protectorPrice*protector;
        double sales  = headphonesales+shellsales+protectorsales;
        if(sales>1800){
            commission = 0.10*1000;
            commission = commission+0.15*800;
            commission = commission+0.20*(sales-1800);
        }else if(sales>1000){
            commission = 0.10*1000;
            commission = commission+0.15*(sales-1000);
        }
        else 
            commission = 0.10*sales;
        
        return commission;
    }
}

2)依据覆盖技术,测试用例列表:

DD-路径图

技术分享

<1>语句覆盖

编号 输入 预期输出 实际输出 执行路径 是否通过
1 10 101 0 222.0 222.0 ABF
2 10 100 0 220.0 220.0 ACDF
3 0 100 0 100.0 100.0 ACEF

<2>判定/条件覆盖

编号 输入 预期输出 实际输出 执行路径 是否通过
1 10 101 0 222.0 222.0 ABF
2 10 100 0 220.0 220.0 ACDF
3 0 100 0 100.0 100.0 ACEF
4 1 100 0 112.0 112.0 ACDF
5 1 1 1 9.8 9.8 ACEF
6 -1 -1 -1 错误提示 -9.8 ACEF ×

 

 

<3>组合覆盖

编号 输入 预期输出 实际输出 执行路径 是否通过
1 10 101 0 222.0 222.0 ABF
2 10 100 0 220.0 220.0 ACDF
3 0 100 0 100.0 100.0 ACEF
4 1 100 0 112.0 112.0 ACDF
5 1 1 1 9.8 9.8 ACEF
6 -1 -1 -1 错误提示 -9.8 ACEF ×
7 0 0 0 0.0 0.0 ACEF

 

3)相应Junit测试脚本、执行结果

package test;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import daima.yongjin01;

public class CommissionTest {

	private yongjin01 yj;
	@Before
	public void setUp() throws Exception {
		yj = new yongjin01();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCommissionSentence1() {
		assertEquals(222.0, yj.Commission(10, 101, 0), 0.00001);
	}
	@Test
	public void testCommissionSentence2() {
		assertEquals(220.0, yj.Commission(10, 100, 0), 0.00001);
	}
	@Test
	public void testCommissionSentence3() {
		assertEquals(100.0, yj.Commission(0, 100, 0), 0.00001);
	}
	@Test
	public void testCommissionCondition1() {
		assertEquals(112.0, yj.Commission(1, 100, 0), 0.00001);
	}
	@Test
	public void testCommissionCondition2() {
		assertEquals(9.8, yj.Commission(1, 1, 1), 0.00001);
	}
	@Test
	public void testCommissionCondition3() {
		assertEquals(-1, yj.Commission(-1, -1, -1), 0.00001);
	}
	@Test
	public void testCommissionCombination() {
		assertEquals(0.0, yj.Commission(0, 0, 0), 0.00001);
	}
}

  结果:

技术分享

4)给出测试参数化和打包测试的脚本,并生成执行结果

测试参数化:

package test;

import static org.junit.Assert.*;

import java.lang.reflect.Array;
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;

import daima.yongjin01;


@RunWith(Parameterized.class)
public class ParameterizedTest {
	private double headphone;
	private double shell;
	private double protector;
	private double result;
	private yongjin01 yj;
	
	@Parameters
	public static Collection data(){
		return Arrays.asList(
			new Object[][]{
				{10, 101, 0, 222.0},
				{10, 100, 0, 220.0},
				{0, 100, 0, 100.0},
				{1, 100, 0, 112.0},
				{1, 1, 1, 9.8},
				{-1, -1, -1, -1},
				{0, 0, 0, 0.0},
			}
		);
	}
	
	public ParameterizedTest (double headphone,double shell,double protector,double result) {
		yj=new yongjin01();
		this.headphone=headphone;
		this.shell=shell;
		this.protector=protector;
		this.result=result;
	}
	
	@Test
	public void testCommission(){
		assertEquals(result,yj.Commission(headphone, shell, protector), 0.00001);
	}
}

  结果:

技术分享

打包测试:

package test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({CommissionTest.class,ParameterizedTest.class})
public class AllCommission {

}

  结果:

技术分享

 

实验二+063+陈彧