某程序中有代码段如下,请设计测试用例,编写测试脚本,完成逻辑覆盖测试。 public class get_ret { public int get_the_ret (int x, int y) {int z = 1; int ret = 0; if(x<0) { z = y-x; } //语句块 1 else { z= y+x; } //语句块 2 if(z>10 && y>0) { ret = z*y; } //语句块 3 else { ret = z*x; } //语句块 4 return ret; //语句块 5 } } 需要完成下面三个任务: 1.画出程序流程图 2.设计测试用例。 (1)语句覆盖:设计测试用例如下。 (2)判定覆盖:本程序有两个判定,设计测试用例如下: (3)条件覆盖:本程序共 3 个条件,设计测试用例如下: (4)判定/条件覆盖:本程序共 2 个判定和 3 个条件,设计测试用例 (5)条件组合覆盖:本程序中有 3 个条件,其中后 2 个条件需要组合
时间: 2023-06-18 09:05:56 浏览: 105
1. 程序流程图如下:
![程序流程图](https://cdn.luogu.com.cn/upload/image_hosting/b3kl5r3f.png)
2. 设计测试用例:
(1)语句覆盖:
| 测试用例 | 输入参数 | 预期输出 |
| -------- | -------- | -------- |
| 1 | x=1, y=2 | 3 |
| 2 | x=-1, y=10 | 9 |
| 3 | x=0, y=5 | 5 |
(2)判定覆盖:
| 测试用例 | 输入参数 | 预期输出 |
| -------- | -------- | -------- |
| 1 | x=1, y=2 | 3 |
| 2 | x=-1, y=10 | -10 |
| 3 | x=-1, y=-10 | 9 |
| 4 | x=0, y=5 | 0 |
(3)条件覆盖:
| 测试用例 | 输入参数 | 预期输出 |
| -------- | -------- | -------- |
| 1 | x=-1, y=-10 | 9 |
| 2 | x=1, y=-10 | -10 |
| 3 | x=-1, y=10 | 9 |
| 4 | x=1, y=10 | 110 |
| 5 | x=0, y=5 | 0 |
(4)判定/条件覆盖:
| 测试用例 | 输入参数 | 预期输出 |
| -------- | -------- | -------- |
| 1 | x=1, y=2 | 3 |
| 2 | x=-1, y=10 | -10 |
| 3 | x=-1, y=-10 | 9 |
| 4 | x=0, y=5 | 0 |
| 5 | x=1, y=-10 | -10 |
| 6 | x=1, y=10 | 110 |
| 7 | x=-1, y=5 | -5 |
(5)条件组合覆盖:
| 测试用例 | 输入参数 | 预期输出 |
| -------- | -------- | -------- |
| 1 | x=-1, y=-10 | 9 |
| 2 | x=1, y=-10 | -10 |
| 3 | x=-1, y=10 | 9 |
| 4 | x=1, y=10 | 110 |
| 5 | x=0, y=5 | 0 |
| 6 | x=1, y=9 | 90 |
| 7 | x=-1, y=9 | 8 |
| 8 | x=1, y=11 | 132 |
| 9 | x=-1, y=11 | 10 |
3. 编写测试脚本:
使用JUnit框架编写测试脚本,代码如下:
```java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class get_retTest {
private get_ret obj = new get_ret();
// 语句覆盖
@Test
public void testStatementCoverage() {
assertEquals(3, obj.get_the_ret(1, 2));
assertEquals(9, obj.get_the_ret(-1, 10));
assertEquals(5, obj.get_the_ret(0, 5));
}
// 判定覆盖
@Test
public void testDecisionCoverage() {
assertEquals(3, obj.get_the_ret(1, 2));
assertEquals(-10, obj.get_the_ret(-1, 10));
assertEquals(9, obj.get_the_ret(-1, -10));
assertEquals(0, obj.get_the_ret(0, 5));
}
// 条件覆盖
@Test
public void testConditionCoverage() {
assertEquals(9, obj.get_the_ret(-1, -10));
assertEquals(-10, obj.get_the_ret(1, -10));
assertEquals(9, obj.get_the_ret(-1, 10));
assertEquals(0, obj.get_the_ret(0, 5));
assertEquals(-10, obj.get_the_ret(1, -10));
}
// 判定/条件覆盖
@Test
public void testDecisionConditionCoverage() {
assertEquals(3, obj.get_the_ret(1, 2));
assertEquals(-10, obj.get_the_ret(-1, 10));
assertEquals(9, obj.get_the_ret(-1, -10));
assertEquals(0, obj.get_the_ret(0, 5));
assertEquals(-10, obj.get_the_ret(1, -10));
assertEquals(110, obj.get_the_ret(1, 10));
assertEquals(-5, obj.get_the_ret(-1, 5));
}
// 条件组合覆盖
@Test
public void testConditionCombinationCoverage() {
assertEquals(9, obj.get_the_ret(-1, -10));
assertEquals(-10, obj.get_the_ret(1, -10));
assertEquals(9, obj.get_the_ret(-1, 10));
assertEquals(0, obj.get_the_ret(0, 5));
assertEquals(-10, obj.get_the_ret(1, -10));
assertEquals(90, obj.get_the_ret(1, 9));
assertEquals(8, obj.get_the_ret(-1, 9));
assertEquals(132, obj.get_the_ret(1, 11));
assertEquals(10, obj.get_the_ret(-1, 11));
}
}
```