软件测试过程与方法_第4章单元测试4.2代码

第4章单元测试代码

  • 方案一
  • 方案二
  • 方案三

软件测试过程与方法_第4章单元测试4.2代码_第1张图片

方案一

方案一:通过main函数,从外部输入体重和身高,创建被测对象,调用被测方法,结果输出到屏幕
软件测试过程与方法_第4章单元测试4.2代码_第2张图片

package sample.ut;

import java.util.Scanner;

/**
*       @description:该类的功能是根据体重和身高判断所属健康分类
*       @Author: user
*       @date: 2020年3月24日17:04:52
* */

public class BMI {
    //定义属性
    private double weight;//体重
    private double height;//身高

    //定义Getter 和Setter
    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //一次性设置体重和身高
    public void setParams(double w, double h){
        this.weight = w;
        this.height = h;
    }

    //定义构造
    public BMI(double w, double h){
        this.weight = w;
        this.height = h;
    }

    // 定义功能方法
    public String getBMIType(){
        //1.初始化
        String result = "";
        double bmi = 0.0;


        if(weight > 0 && height > 0){
            //2.计算BMI
            bmi = weight / (height * height);

            //3.判断所属分类
            if(bmi <18.5) {
                result = "偏瘦";
            }else  if(bmi < 24){
                result = "正常";
            }else if (bmi < 28) {
                result = "偏胖";
            }else{
                result = "肥胖";
            }

        }else {
            result = "体重和身高数据错误";
        }

        //4.返回分类
        return result;
    }

    public static void main(String[] args) {

     
        //        方案一:通过main函数,从外部输入体重和身高,创建被测对象,调用被测方法,结果输出到屏幕
 //1.输入体重和身高
        Scanner sc = new Scanner(System.in);
        double w = 0.0;
        double h = 0.0;
        System.out.println("请输入体重和身高,以等号结束:");
        while (sc.hasNextDouble()){
            w = sc.nextDouble();
            h = sc.nextDouble();
        }

        //2.创建被测对象
        BMI test = new BMI(w,h);

        //3.调用被测方法
        String result = test.getBMIType();

        //4.结果输出到屏幕
        System.out.println(result);





        BMI test = new BMI(45.0 , 1.6);
        System.out.println(test.getBMIType());


        test.setParams(55.0, 1.6);
        System.out.println(test.getBMIType());

        test.setParams(68.0, 1.6);
        System.out.println(test.getBMIType());

        test.setParams(80.0, 1.6);
        System.out.println(test.getBMIType());
    }
}

方案二

//方案2: 通过main函数,由脚本自行设置体重和身高
//创建被测对象,调用被测方法,并自行校验结果,判断测试结果,缺陷输出到屏幕
软件测试过程与方法_第4章单元测试4.2代码_第3张图片

package sample.ut;

import java.util.Scanner;

/**
*       @description:该类的功能是根据体重和身高判断所属健康分类
*       @Author: user
*       @date: 2020年3月24日17:04:52
* */

public class BMI {
    //定义属性
    private double weight;//体重
    private double height;//身高

    //定义Getter 和Setter
    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    //一次性设置体重和身高
    public void setParams(double w, double h){
        this.weight = w;
        this.height = h;
    }

    //定义构造
    public BMI(double w, double h){
        this.weight = w;
        this.height = h;
    }

    // 定义功能方法
    public String getBMIType(){
        //1.初始化
        String result = "";
        double bmi = 0.0;


        if(weight > 0 && height > 0){
            //2.计算BMI
            bmi = weight / (height * height);

            //3.判断所属分类
            if(bmi <18.5) {
                result = "偏瘦";
            }else  if(bmi < 24){
                result = "正常";
            }else if (bmi < 28) {
                result = "偏胖";
            }else{
                result = "肥胖";
            }

        }else {
            result = "体重和身高数据错误";
        }

        //4.返回分类
        return result;
    }

    public static void main(String[] args) {

        //方案2: 通过main函数,由脚本自行设置体重和身高
        //创建被测对象,调用被测方法,并自行校验结果,判断测试结果,缺陷输出到屏幕
        //1. 创建被测对象,设置体重和身高
        BMI test = new BMI(45.0, 1.6);

        //2.调用被测方法
        String result = test.getBMIType();

        //3.校验结果,判断测试结果
        String expect = "偏瘦";
        String output = "";
        if(expect == result){
            output += "PASS";

        }else{
            output += "FALL, 体重:45.0, 身高: 1.6,预期: " + expect + ",实际返回:" + result;
        }

        //4.如有缺陷,输出到屏幕
        System.out.println(output);
        test = null;

        //测试用例2: 55.0, 1.6
        test = new BMI(55.0, 1.6);
        result = test.getBMIType();
        expect = "正常";
        output = "";
        if(result == expect){
            output += "PASS";
        }else {
            output +=  "FALL, 体重:45.0, 身高: 1.6,预期: " + expect + ",实际返回:" + result;
        }
        System.out.println(output);
        test = null;

        //测试用例3: 68.0, 1.6
        test = new BMI(68.0, 1.6);
        result = test.getBMIType();
        expect = "偏胖";
        output = "";
        if(result == expect){
            output += "PASS";
        }else {
            output +=  "FALL, 体重:45.0, 身高: 1.6,预期: " + expect + ",实际返回:" + result;
        }
        System.out.println(output);
        test = null;

        //测试用例4 80.0, 1.6
        test = new BMI(80.0, 1.6);
        result = test.getBMIType();
        expect = "肥胖";
        output = "";
        if(result == expect){
            output += "PASS";
        }else {
            output +=  "FALL, 体重:45.0, 身高: 1.6,预期: " + expect + ",实际返回:" + result;
        }
        System.out.println(output);
        test = null;

    }
}

方案三

软件测试过程与方法_第4章单元测试4.2代码_第4张图片

import sample.ut.BMI;

import java.util.SplittableRandom;

/**
 * @description BMI的测试类
 * @author wuzhichun
 * @date 2020/3/25 20:16
 */



public class TestBMI {
    //定义属性
    BMI testObj;//被测类
    //定义Getter 和 Setter

    //定义构造方法
    public TestBMI() {
        testObj = null;
    }

    //定义功能方法



    //创建被测方法
    public void createObject(double w, double h) {
        testObj = new BMI(w,h);
    }

    //销毁被测对象
    public void destroyObject(){
        testObj = null;
    }

    //校验执行结果
    public boolean verify(String expect, String actual) {
        if(expect == actual){
            return true;
        }else {
            return false;
        }
    }

    //记录执行过程:测试结果,输入,预期结果,实际输出
    public String record(boolean  testResult, double w, double h, String expect, String actual){
        //如果测试结果是通过,测直接返回PASS, 否则,返回FAIL, 并记录相关数据
        String output = "";
        if(testResult) {
            output += "PASS.";
        }else {
            output += "FAIL." +
                    "体重:" +
                    ",身高:" +
                    ",预期:" +
                    ",实际输出:" + actual;
        }
        return output;
    }

    //定义测试方法
    //测试用例1:45,1.6
    public void testGetBMIType1(){
        //1.创建被测对象
        createObject(45.0, 1.6);

        //2.调用别册方法
        String actual = testObj.getBMIType();

        //3.校验执行结果
        String expected = "偏瘦";
        boolean testResult = verify(expected, actual);

        //4.记录执行过程
        String output = record(testResult, 45.0, 1.6, expected, actual);

        //5.结果输出到屏幕
        System.out.println(output);

        //6.销毁被测对象
        destroyObject();
    }


    //测试用例2:55.0, 1.6
    public void testGetBMIType2(){
        createObject(55, 1.6);
        String actual = testObj.getBMIType();
        String exp = "正常";
        boolean tr = verify(exp, actual);
        String  out = record(tr, 55, 1.6, exp, actual);
        System.out.println(out);
        destroyObject();
    }

    //测试用例3:68.0, 1.6
    public void testGetBMIType3(){
        createObject(68, 1.6);
        String actual = testObj.getBMIType();
        String exp = "偏胖";
        boolean tr = verify(exp, actual);
        String  out = record(tr, 55, 1.6, exp, actual);
        System.out.println(out);
        destroyObject();
    }

    //测试用例4:80.0, 1.6
    public void testGetBMIType4(){
        createObject(80, 1.6);
        String actual = testObj.getBMIType();
        String exp = "肥胖";
        boolean tr = verify(exp, actual);
        String  out = record(tr, 55, 1.6, exp, actual);
        System.out.println(out);
        destroyObject();
    }

    public static void main(String[] args){
        TestBMI test = new TestBMI();
        test.testGetBMIType1();
        test.testGetBMIType2();
        test.testGetBMIType3();
        test.testGetBMIType4();
    }

}

你可能感兴趣的:(软件测试过程与方法)