单元测试—BMI脚本设计

news2025/6/11 7:05:59

BMI例题如下:

BMI中国计算标准:体质指数(BMI)=体重(kg)÷身高^2(m)

例如:一个人的身高为1.75米,体重为68千克,他的BMI=68/(1.75^2)=22.2(千克/米^2)当BMI指数为18.5~23.9时属正常。

成人的BMI数值标准:

过轻:低于18.5         正常:>=18.5且<24

过重:>=24且<28        肥胖:>=28且<32

非常肥胖:>=32

1步:在IEDA环境下完成BMI的代码实现

方案1:通过键盘输入身高,体重或者直接通过构造方法或者BMI类的成员方法初始化身高体重,然后调用方法计算BMI值,并人工判断校验。

package sample;
import java.util.Scanner;
import java.text.DecimalFormat;
import static java.lang.Math.abs;
public class BMI {
    double height; //身高
    double weight; //体重
    //设置和得到属性值
    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 BMI(double w, double h) {
        weight = w;
        height = h;
    }

    //设置体重和身高
    public void setParams(double w, double h) {
        weight = w;
        height = h;
    }
//根据 BMI 值判断健康状况
    public String getBMIType() {
        double bmi = 0.0;
        String result = "";
//设置浮点数输出格式,保留 2 位小数
        DecimalFormat df = new DecimalFormat("#.00");
        if (weight > 0 && height > 0) {
//计算 BMI
            bmi = weight / (height * height);
//2、根据 bmi 判断所属健康分类
            if (bmi < 18.5) {
                result = "偏瘦";
            } else if (bmi < 24) {
                result = "正常";
            } else if (bmi < 28) {
                result = "过重";
            }else if (bmi < 32) {
                result = "肥胖";
            }else {
                result = "非常肥胖";
            }        } else {
            return "重量或者身高错误!";
        }
        System.out.println("bmi 的值是:" + df.format(bmi));
        return result;
    }

public static void main(String[] args) {
//方案 1
//用户输入体重和身高,调用被测方法,结果输出到屏幕
//得到一个扫描对象,从键盘接收数据
        Scanner reader = new Scanner(System.in);
        double w = 0.0, h = 0.0;
        System.out.println("请输入体重(公斤)和身高(米),以等号=结束");
        //检测到下一个数为 Double 类型,则返回 True
        while (reader.hasNextDouble()) {
            w = reader.nextDouble();
            h = reader.nextDouble();
        }
BMI testobj = new BMI(w, h);
        String result = testobj.getBMIType();
        String output = "体重:" + w + ",身高:" + h + ",BMI 状况是:" + result;
        System.out.println(output);
        //设置多个测试用例
        BMI tmpobj = new BMI(45.0, 1.6);
        String type = tmpobj.getBMIType();
        System.out.println(type);
        tmpobj.setParams(55, 1.6);
        System.out.println(tmpobj.getBMIType());
        tmpobj.setParams(68, 1.6);
        System.out.println(tmpobj.getBMIType());
        tmpobj.setParams(80, 1.6);
        System.out.println(tmpobj.getBMIType());
  }
}

根据自身实际问题再做修改。

2步:针对BMI类设计测试用例

输入

BMI值

等价类/边界值

预期输出

用例编号

体重(KG)

身高(M)

1

59.95

1.80

18.5

等于边界值18.5

正常

2

46.24

1.70

16

输出等价类小于18.5

过轻

3

0

1.70

输入体重边界值0

输入有误

4

48.91

1.62

18.6

输出等价类大于18.5

正常

5

69.12

1.70

23.9

输出等价类小于24

正常

6

68.55

1.69

24.0

等于边界值24

过重

7

71.32

1.72

24.1

输出等价类大于24

过重

8

82.56

1.72

27.9

输出等价类小于28

过重

9

79.10

1.68

28.0

等于边界值28

肥胖

10

80.31

1.69

28.1

输出等价类大于28

肥胖

11

93.31

1.71

31.9

输出等价类小于32

肥胖

12

88.20

1.66

32.0

等于边界值32

非常肥胖

13

88.50

1.66

32.1

输出等价类大于32

非常肥胖

14

100.04

1.69

35.0

输出等价类大于32

非常肥胖

15

60.00

0

输入身高边界值0

输入有误

16

200.00

1.7

69.2

无效等价类超出正常体重

输入有误

17

60.00

2.50

9.6

无效等价类超出正常身高

输入有误

18

60.00

0.90

74.1

无效等价类低于正常身高

输入有误

设计用例不多,可以再自行增加边界值用例。

第3

方案2是在方案1的基础上改进,将预期值和计算的BMI值进行比较,实现自动校验。

package sample;
import java.util.Scanner;
import java.text.DecimalFormat;
import static java.lang.Math.abs;
public class BMI {
    double height; //身高
    double weight; //体重
    //设置和得到属性值
    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 BMI(double w, double h) {
        weight = w;
        height = h;
    }

    //设置体重和身高
    public void setParams(double w, double h) {
        weight = w;
        height = h;
    }
//根据 BMI 值判断健康状况
    public String getBMIType() {
        double bmi = 0.0;
        String result = "";
//设置浮点数输出格式,保留 2 位小数
        DecimalFormat df = new DecimalFormat("#.00");
        if (weight > 0 && height > 0) {
//计算 BMI
            bmi = weight / (height * height);
//2、根据 bmi 判断所属健康分类
            if (bmi < 18.5) {
                result = "偏瘦";
            } else if (bmi < 24) {
                result = "正常";
            } else if (bmi < 28) {
                result = "过重";
            }else if (bmi < 32) {
                result = "肥胖";
            }else {
                result = "非常肥胖";
            }        } else {
            return "重量或者身高错误!";
        }
        System.out.println("bmi 的值是:" + df.format(bmi));
        return result;
    }

public static void main(String[] args) {
//方案 2:脚本自行根据测试用例来设置体重和身高,并自动校验执行结果
//1、创建被测对象
BMI testobj=new BMI(48.91,1.62);
    //2、调用被测方法
    String actual=testobj.getBMIType();
    //3、校验执行结果
    String expected="正常";
    String output="";
if(actual==expected)
    { output+="pass";
    }else
    { output+="Fail,体重:48.91,身高 1.62,Expected:"+expected+",Actual:"+actual;
    }output+="\n";
//测试用例 2
testobj.setParams(69.12,1.70); actual=testobj.getBMIType(); expected="正常";
if(actual==expected)
    {
        output+="pass";
    }else
    { output+="Fail,体重:69.12,身高 1.70,Expected:"+expected+",Actual:"+actual;
    }output+="\n";
//测试用例 3
testobj.setParams(68.55,1.69); actual=testobj.getBMIType(); expected="过重";
if(actual==expected)
    { output+="pass";
    }else
    { output+="Fail,体重:68.55,身高 1.69,Expected:"+expected+",Actual:"+actual;
    }output+="\n";
//测试用例 4
testobj.setParams(71.32,1.72); actual=testobj.getBMIType(); expected="过重";
if(actual==expected)
    { output+="pass";
    }else
 { output+="Fail,体重:71.32,身高 1.72,Expected:"+expected+",Actual:"+actual;
    }output+="\n";
//4、输出结果
System.out.println(output);
    }
}

第4

先另外创建一个TestBMI类,在方案1和方案2基础上做如下改进:

方案3代码如下:

package sample;
import static java.lang.Math.abs;
class TestBMI {
    BMI bmiObj; //被测类
    //创建被测对象
    public void createTestobj(double w, double h) {
        bmiObj = new BMI(w, h);
    }
    //释放被测对象
    public void freeTestobj() {
        bmiObj = null;
    }
//执行结果校验
    public boolean verify(String expected, String actual) {
        if (expected == actual) { return true;
        } else {return false;
        }
    }
    //记录执行过程
    public String record(double w, double h, String expected, String actual, boolean testResult) {
        String output = "";
        if (testResult) { output += "Pass. 体重:" + w + ", 身高:" + h;
        } else {output += "Fail. 体重:" + w + ", 身高:" + h +
                ", Expected:" + expected + ", Actual:" + actual;
        }return output;
    }
    //测试用例 1
    public void testGetBMIType1() { createTestobj(48.91, 1.62);
        String actual = bmiObj.getBMIType();
        boolean testResult = verify("正常", actual);
        System.out.println(record(48.91, 1.62, "正常", actual, testResult));
        freeTestobj();
    }
    //测试用例 2
    public void testGetBMIType2() { createTestobj(69.12, 1.70);
        String actual = bmiObj.getBMIType();
        boolean testResult = verify("正常", actual);
        System.out.println(record(69.12, 1.70, "正常", actual, testResult));
        freeTestobj();
    }
    //测试用例 3
    public void testGetBMIType3() { createTestobj(68.55, 1.69);
        String actual = bmiObj.getBMIType();
        boolean testResult = verify("过重", actual);
        System.out.println(record(68.55, 1.69, "过重", actual, testResult));
        freeTestobj();
    }
//测试用例 14
    public void testGetBMIType4() { createTestobj(71.32, 1.72);
        String actual = bmiObj.getBMIType();
        boolean testResult = verify("过重", actual);
        System.out.println(record(71.32, 1.72, "过重", actual, testResult));
        freeTestobj();
    }
    //主函数
    public static void main(String[] args) {
        TestBMI test = new TestBMI();
        test.testGetBMIType1();
        test.testGetBMIType2();
        test.testGetBMIType3();
        test.testGetBMIType4();
    }
}

5步:BMI类下创建BMITest类进行独立测试

测试结果如下:

代码如下:

package sample;
import org.junit.Test;
import static org.junit.Assert.*;
public class BMITest {
    BMI testobj; //创建被测类
    @Test
    public void getBMIType() {
//创建被测对象
        testobj=new BMI(48.91,1.62);
        String expected="正常";
//System.out.println(testobj.getBMIType());
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType()==expected);
        testobj=null;
    }
    @Test
    public void getBMIType_Normal() {
//创建被测对象
        testobj=new BMI(69.12,1.70);
        String expected="正常";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType()==expected);
        testobj=null;
    }
    @Test
    public void getBMIType_Thin() {
//创建被测对象
        testobj=new BMI(68.55,1.69);
        String expected="过重";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType()==expected);
        testobj=null;
}
@Test
    public void getBMIType_SlightlyFat() {
//创建被测对象
testobj=new BMI(71.32,1.72);
        String expected="过重";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType()==expected);
//释放对象
        testobj=null;
    }
    @Test
    public void getBMIType_Fat() {
//创建被测对象
        testobj=new BMI(79.1,1.68);
        String expected="肥胖";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType()==expected);
        testobj=null;
    }
}

第6步:BMI类创建BMITest1类,使用Before和After方法进行独立测试

测试结果如下:

代码如下:

package sample;
import org.junit.*;
import static org.junit.Assert.*;
public class BMITest1 {
    BMI testobj;
    @Before
    public void setUp() {
        System.out.println("Run @Before method");
        testobj = new BMI();
    }
    @After
    public void tearDown() {
        System.out.println("Run @After method");
        testobj = null;
    }
    @BeforeClass
    public static void prepareEnvironment() {
        System.out.println("Run @BeforeClass Method");
    }
    @AfterClass
    public static void RestoreEnvironment() {
        System.out.println("Run @AfterClass Method");
    }
    @Test
    public void getBMIType() {
//创建被测对象
        testobj.setParams(55.0, 1.6);
        String expected = "正常";
//System.out.println(testobj.getBMIType());
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType() == expected);
}
@Test
    public void getBMIType_Normal() {
//赋值被测对象
        testobj.setParams(55.0, 1.6);
        String expected = "正常";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType() == expected);
    }
@Test
    public void getBMIType_Thin() {
//赋值被测对象
        testobj.setParams(45.0, 1.6);
        String expected = "偏瘦";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType() == expected);
    }
    @Test
    public void getBMIType_SlightlyFat() {
//赋值被测对象
        testobj.setParams(55.0, 1.6);
        String expected = "正常";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType() == expected);
    }
    @Test
    public void getBMIType_Fat() {
//赋值被测对象
        testobj.setParams(80.0, 1.6);
        String expected = "肥胖";
//调用测试方法,并校验测试结果
        assertTrue(testobj.getBMIType() == expected);
    }
}

代码根据需求或用例自行修改。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1681993.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

单链表经典算法 面试题--力扣02.04

链接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09;【点击即可跳转】 思路&#xff1a;创建新链表&#xff1a;大链表和小链表 将pcur节点小于x的值&#xff0c;尾插在小链表中 将pcur节点大于或等于x的值&#xff0c;尾插在大链表中 最终---- return lessHead->…

【Maven】Nexus简单使用

1、安装配置介绍Nexus私服&#xff1a; 安装配置指路上一篇详细教程博客 【Maven】Nexus私服简介_下载安装_登录-CSDN博客 简单介绍原有仓库类型&#xff1a; proxy代理仓库&#xff1a;代理远程仓库&#xff0c;访问全球中央仓库或其他公共仓库&#xff0c;将资源存储在私…

【工具】macOS、window11访问limux共享目录\共享磁盘,samba服务安装使用

一、samba服务安装 Samba是一个免费的开源软件实现&#xff0c;使得非Windows操作系统能够与Windows系统进行文件和打印服务共享。它实现了SMB/CIFS协议&#xff0c;并且能够在Linux、Unix、BSD等多种系统上运行。 安装 samba&#xff1a; sudo yum install samba配置 samba…

全球知名哲学家思想家颜廷利:将人生黑暗视为一种机遇

在时间的长河中&#xff0c;我们短暂的人生不过是眨眼间的光景。然而&#xff0c;正是这短暂的旅程给予了我们无限的可能性和转变的契机。我们应该勇敢地面对生活中的暗夜&#xff0c;将其视作成长的土壤&#xff0c;让自我在其中焕发出独特的光辉。 当我们在生命的历程中暂停脚…

基于ASN.1的RSA算法公私钥存储格式解读

1.概述 RFC5958主要定义非对称密钥的封装语法&#xff0c;RFC5958用于替代RFC5208。非对称算法会涉及到1对公私钥&#xff0c;例如按照RSA算法&#xff0c;公钥是n和e&#xff0c;私钥是d和n。当需要将公私钥保存到文件时&#xff0c;需按照一定的格式保存。本文主要定义公私钥…

002_Anaconda的安装与使用

Python的开发环境 官方介绍&#xff1a;Anaconda&#xff0c;中文大蟒蛇&#xff0c;是一个开源的Python发行版本&#xff0c;其包含了conda、Python等180多个科学包及其依赖项。 比较抽象&#xff0c;看不懂没有关系&#xff0c;慢慢往下看。 很多学习python的初学者甚至学…

Android 触摸事件分离原理

什么是触摸事件分离&#xff1f; 屏幕上存在多个窗口时&#xff0c;多指触摸的情况下&#xff0c;多个手指的触摸事件可以分给不同的窗口&#xff0c;以下面的图为例&#xff0c;第一个手指按下&#xff0c;window1可以响应这个事件&#xff0c;第二个手指按下&#xff08;第一…

Vue的学习 —— <vue组件>

目录 前言 正文 一、选项式API与组合式API 二、生命周期函数 1、onBeforeMount() 2、onMounted() 3、onBeforeUpdate() 4、onUpdated() 5、onBeforeUnmount() 6、onUnmounted() 三、组件之间的样式冲突 四、父组件向子组件传递数据 1、定义props 2、静态绑定props…

Elasticsearch 在滴滴的应用与实践

滴滴 Elasticsearch 简介 简介 Elasticsearch 是一个基于 Lucene 构建的开源、分布式、RESTful 接口的全文搜索引擎&#xff0c;其每个字段均可被索引&#xff0c;且能够横向扩展至数以百计的服务器存储以及处理 TB 级的数据&#xff0c;其可以在极短的时间内存储、搜索和分析大…

文章解读与仿真程序复现思路——中国电机工程学报EI\CSCD\北大核心《集装箱海港级联物流-能源耦合系统协同优化方法 》

本专栏栏目提供文章与程序复现思路&#xff0c;具体已有的论文与论文源程序可翻阅本博主免费的专栏栏目《论文与完整程序》 论文与完整源程序_电网论文源程序的博客-CSDN博客https://blog.csdn.net/liang674027206/category_12531414.html 电网论文源程序-CSDN博客电网论文源…

jenkins配置不同版本nodeJS,保姆级叫你配置

问题描述&#xff1a;公司jenkins被改了nodejs版本适配其他项目导致以前的项目构建失败&#xff0c;原因就是nodejs版本太高或太低导致&#xff0c;这里教大家不去更改服务器默认版本&#xff0c;当需要特殊版本直接在jenkins里配置即可。 过程 1、安装nodeJS插件 1.1点击管…

XML文件转TXT文件 yolo标签转换(代码可直接使用) 可批量转换

像这样的xml文件&#xff0c;我们可以通过代码批量转换为txt文件格式&#xff1a; 新建一个xml2txt.py文件&#xff0c; 上代码&#xff0c;直接复制粘贴 import xml.etree.ElementTree as ET import osdef convert(size, box):x_center (box[0] box[1]) / 2.0y_center (box…

2020 年第一届辽宁省大学生程序设计竞赛

比赛经历&#xff1a;摸鱼划水了一个多小时又是只会签到&#xff0c;看来还得提升自己的解题能力写了六题 补题&#xff1a;E线段树维和区间平方和&#xff0c;比较经典好久没写过线段树了傻了&#xff0c;注意维护lazy J计算几何&#xff0c;看来得提上日程了&#xff0c;用叉…

0.98T优于10米高程DEM数据

我们在《全球30米100%水陆覆盖高程》一文中&#xff0c;为大家分享了全球100%覆盖&#xff0c;且包括海底高程的30米DEM数据。 该数据虽然全球无死角覆盖&#xff0c;但分辨率只有30米。 这里&#xff0c;再为大家分享一个优于10米的高程数据&#xff0c;但目前仅覆盖全国范围…

The 13th Shandong ICPC Provincial Collegiate Programming Contest

The 13th Shandong ICPC Provincial Collegiate Programming Contest The 13th Shandong ICPC Provincial Collegiate Programming Contest A. Orders 题意&#xff1a;有n个订单&#xff0c; 每日可生产k个产品&#xff0c;每个订单给出交付日和交付数量&#xff0c;是否能…

每日一练 2024.5.16(补 2024.5.14)

题目&#xff1b; 我们定义 arr 是 山形数组 当且仅当它满足&#xff1a; arr.length > 3存在某个下标 i &#xff08;从 0 开始&#xff09; 满足 0 < i < arr.length - 1 且&#xff1a; arr[0] < arr[1] < ... < arr[i - 1] < arr[i]arr[i] > arr…

泛微E9开发 自动获取日期

选择开始日期&#xff0c;自动获取指定天数后的结束日期 1、功能背景2、展示效果3、实现方法 1、功能背景 用户选择开始日期&#xff0c;系统自动带出结束日期&#xff0c;如需要带出6天后的结束日期&#xff0c;下图所示&#xff0c;5月14日到5月20日是一个周期&#xff0c;用…

Linux-centos下安装ffmpeg的详细教程

源安装 第一种方式&#xff1a; . 首先需要安装yum源&#xff1a; 这个源安装的ffmpeg版本是3.4 yum install epel-release yum install -y https://mirrors.ustc.edu.cn/rpmfusion/free/el/rpmfusion-free-release-7.noarch.rpm然后可以安装ffmpeg yum install -y ffmpeg ff…

电阻式传感器

电阻式传感器是一种将非电学量&#xff08;如温度、压力、位移等&#xff09;转换为电阻变化的传感器。它大致分为电阻应变式传感器、压阻式传感器和变阻式传感器三类。 电阻式传感器的优点包括结构简单、成本低廉、稳定性好&#xff0c;适用于多种环境。但它们也有局限性&…

第十四届蓝桥杯大赛软件赛国赛C/C++ 大学 B 组 AB路线

//bfs 1000100010不会超时 #include<bits/stdc.h> using namespace std; #define int long long const int n1e311; int a,b,c,h[n][n][12],k[4][2]{0,1,0,-1,1,0,-1,0}; char t[n][n]; struct s {int x,y,z,w; }; signed main() {ios::sync_with_stdio(false);cin.t…