0


单元测试—血压诊断代码设计

血压诊断标准如下:

低血压(收缩压<90 或 舒张压<60)

正常高值(120<=收缩压<=139和 80<=舒张压<=89 )

正常血压(90<=收缩压<=130和60<=舒张压<=90)

1级高血压(140<=收缩压<=159 或 90<=舒张压<=99)

2级高血压(160<=收缩压<=179 或 100<=舒张压<=109)

3级高血压(180<=收缩压 或 110<=舒张压)

异常值(其它值)

BloodPressure****实现代码如下:

package sample;
public class BloodPressure {
    int systolicProssure; //收缩压
    int diatolicProssure; //舒张压
    //设置属性
    public int getSystolicProssure() { return systolicProssure;
    }
    public void setSystolicProssure(int systolicProssure) {
        this.systolicProssure = systolicProssure;
    }
    public int getDiatolicProssure() { return diatolicProssure;
    }
    public void setDiatolicProssure(int diatolicProssure) {
        this.diatolicProssure = diatolicProssure;
    }
    //设置收缩压和舒张压
    public void setParams(int sysPressure, int diaProssure) { systolicProssure = sysPressure;
        diatolicProssure = diaProssure;
    }
    //构造函数
    public BloodPressure() { systolicProssure = 0;
        diatolicProssure = 0;
    }
    public BloodPressure(int sysPressure, int diaPressure) { systolicProssure = sysPressure;
        diatolicProssure = diaPressure;
    }
   //根据血压值判断所属血压分级
    public String getPressureLevel() {
        String level = "";
        if ( systolicProssure < 90 || diatolicProssure < 60 ) {
            level = "低血压";
        }
        else if ((systolicProssure >= 120 && systolicProssure <= 139) && (diatolicProssure >= 80 &&
                diatolicProssure <= 89)) {
            level = "正常高值";
        }
else if ((90 <= systolicProssure &&  systolicProssure <= 130) && (60 <= diatolicProssure &&
                diatolicProssure <= 90) ){
            level = "正常血压";
        } else if ((systolicProssure >= 180) || (diatolicProssure >= 110)) {
            level = "3级高血压";
        }else if ((systolicProssure >= 160 && systolicProssure <= 179) || (diatolicProssure >= 100 &&
                diatolicProssure <= 109)) {
            level = "2级高血压";
        } else if ((systolicProssure >= 140 && systolicProssure <= 159) || (diatolicProssure >= 90 &&
                diatolicProssure <= 99)) {
            level = "1级高血压";
        }   else {
            level = "异常值";
        }return level;
    }
}

针对BloodPressure设计测试用例。针对BloodPressure的输入域和输出域采取等价类划分法、边界值分析法设计测试用例。

输入

脉压差

等价类/边界值

预期输出

用例编号

收缩压

舒张压

1

90

59

31

血压输入、脉压差边界值

低血压并且脉压差理想

2

120

78

42

血压输入边界值

正常并且脉压差正常

3

110

55

55

血压输入、脉压差等价类

低血压并且脉压差正常

4

79

60

19

血压输入、脉压差边界值

低血压并且脉压差缩小

5

111

50

61

血压输入等价类,脉压差边界值

低血压并且脉压差扩大

6

78

59

19

血压输入、脉压差边界值

低血压并且脉压差缩小

7

120

59

61

血压输入、脉压差边界值

低血压并且脉压差扩大

8

90

60

30

血压输入、脉压差边界值

正常血压并且脉压差理想

9

90

70

20

血压输入、脉压差边界值

正常血压并且脉压差正常

10

90

71

19

血压输入、脉压差边界值

正常血压并且脉压差缩小

11

121

59

62

血压输入边界值、脉压差等价类

低血压并且脉压差扩大

12

130

89

41

血压输入边界值、脉压差等价类

正常高值并且脉压差正常

13

91

61

30

血压输入边界值、脉压差等价类

正常血压并且脉压差理想

14

91

81

10

血压输入边界值、脉压差等价类

正常血压并且脉压差缩小

15

129

60

69

血压输入边界值、脉压差等价类

正常血压并且脉压差扩大

16

129

61

68

血压输入边界值、脉压差等价类

正常血压并且脉压差扩大

17

129

89

40

血压输入、脉压差边界值

正常高值并且脉压差理想

18

129

81

48

血压输入边界值、脉压差等价类

正常高值并且脉压差正常

19

130

60

70

血压输入边界值、脉压差等价类

正常血压并且脉压差扩大

20

130

61

69

血压输入边界值、脉压差等价类

正常血压并且脉压差扩大

21

139

80

59

血压输入、脉压差边界值

正常高值并且脉压差正常

22

139

89

50

血压输入边界值、脉压差等价类

正常高值并且脉压差正常

23

140

89

51

血压输入边界值、脉压差等价类

1级高血压并且脉压差正常

24

141

89

52

血压输入边界值、脉压差等价类

1级高血压并且脉压差正常

25

141

91

50

血压输入边界值、脉压差等价类

1级高血压并且脉压差正常

26

159

89

70

血压输入边界值、脉压差等价类

1级高血压并且脉压差扩大

27

160

89

71

血压输入边界值、脉压差等价类

2级高血压并且脉压差扩大

28

179

89

90

血压输入边界值、脉压差等价类

2级高血压并且脉压差扩大

29

159

91

68

血压输入边界值、脉压差等价类

1级高血压并且脉压差扩大

30

160

91

69

血压输入边界值、脉压差等价类

2级高血压并且脉压差扩大

31

179

91

88

血压输入边界值、脉压差等价类

2级高血压并且脉压差扩大

32

180

110

70

血压输入边界值、脉压差等价类

3级高血压并且脉压差扩大

33

181

110

71

血压输入边界值、脉压差等价类

3级高血压并且脉压差扩大

34

181

111

70

血压输入边界值、脉压差等价类

3级高血压并且脉压差扩大

35

0

60

60

血压输入无效等价类、脉压差边界值

异常值

36

90

0

90

血压输入无效等价类、脉压差等价类

异常值

37

60

血压输入、脉压差无效等价类

异常值

38

90

血压输入、脉压差无效等价类

异常值

血压用例先设计这么多,用例不可能穷尽,只有代表性的用例才有意义。

BloodPressure建立测试类BloodPressureTest。

代码如下:

package sample;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import org.junit.experimental.categories.Category;
import static org.junit.Assert.*;
//指定参数化运行器
@RunWith(Parameterized.class)
public class BloodPressureTest {
    //1、定义属性
    @Parameterized.Parameter(0)
    public int systolicProssure; //收缩压
    @Parameterized.Parameter(1)
    public int diatolicProssure; //舒张压
    @Parameterized.Parameter(2)
    public String expected; //预期结果
    //准备测试数据集
@Parameterized.Parameters(name = "{index}:getPressureLevel[{0},{1}]=[{2}]")
    public static Collection testDataset() { return Arrays.asList(new Object[][]
            {
                    {79, 60, "低血压"}, {90, 60, "正常血压"}, {150, 95, "1级高血压"}, {170, 105, "2级高血压"}, {190, 120, "3级高血压"}
            });
    }
    //定义被测类
    BloodPressure testobj;
    @Before public void setUp() throws Exception {
        testobj = new BloodPressure(systolicProssure, diatolicProssure);
    }
    @After public void tearDown() throws Exception {
        testobj = null;
    }
    @Test
    public void getPressureLevel() { assertTrue(testobj.getPressureLevel() == expected);
    }
}

BloodPressure类测试用例分类。

直接给出代码:

package sample;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.junit.Assert.*;
public class BloodPressureTestByCategory {
    //定义被测类
    BloodPressure testObj;
    @Before public void setUp() throws Exception {
        testObj = new BloodPressure();
    }
    @After public void tearDown() throws Exception {
        testObj = null;
    }
    @Category({EPTest.class})
    @Test
    public void getPressureLevel_Normal() {
        testObj.setParams(110, 50); assertTrue(testObj.getPressureLevel() == "低血压");
    }
    @Category({EPTest.class})
    @Test
    public void getPressureLevel_NormalHigher() {
        testObj.setParams(78, 59); assertTrue("低血压" == testObj.getPressureLevel());
    }
    @Category({EPTest.class})
    @Test
    public void getPressureLevel_FirstLevel() {
        testObj.setParams(150, 95); assertTrue("1级高血压" == testObj.getPressureLevel());
}
@Category({EPTest.class})
    @Test
    public void getPressureLevel_SecondLevel() {
        testObj.setParams(170, 105); assertTrue("2级高血压" == testObj.getPressureLevel());
    }
@Category({EPTest.class})@Test
    public void getPressureLevel_ThirdLevel() {
        testObj.setParams(190, 120); assertTrue("3级高血压" == testObj.getPressureLevel());
    }@Category({BVTTest.class}) @Test
    public void getPressureLevel_Boundary1() {
        testObj.setParams(90, 60); assertTrue("正常血压" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary2() {
        testObj.setParams(120, 85); assertTrue("正常高值" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary3() {
        testObj.setParams(121, 85); assertTrue("正常高值" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary4() {
        testObj.setParams(139, 85); assertTrue("正常高值" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary5() {
        testObj.setParams(140, 85); assertTrue("1级高血压" == testObj.getPressureLevel());
}@Test
    public void getPressureLevel_Boundary6() {
        testObj.setParams(141, 85); assertTrue("1级高血压" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary7() {
        testObj.setParams(159, 85); assertTrue("1级高血压" == testObj.getPressureLevel());
}@Test
    public void getPressureLevel_Boundary8() {
        testObj.setParams(160, 85); assertTrue("2级高血压" == testObj.getPressureLevel());
    }@Test
    public void getPressureLevel_Boundary9() {
        testObj.setParams(179, 85); assertTrue("2级高血压" == testObj.getPressureLevel());
    }

结束。

标签: 单元测试 java

本文转载自: https://blog.csdn.net/m0_74748236/article/details/139044533
版权归原作者 SmoothSailingT 所有, 如有侵权,请联系我们删除。

“单元测试—血压诊断代码设计”的评论:

还没有评论