前置知识
白盒测试用例设计方法(语句覆盖、判定覆盖、条件覆盖、判定/条件覆盖、组合覆盖、路径覆盖、基本路径覆盖)
语句覆盖:每条语句至少执行一次。
判定覆盖:每个判定的所有可能结果至少出现一次。(又称“分支覆盖”)
条件覆盖:每个条件的所有可能结果至少执行一次。
判定/条件覆盖:一个判定中的每个条件的所有可能结果至少执行一次,并且每个判断本身的所有可能结果至少执行一次。
多重条件覆盖(组合覆盖):每个判定中的所有可能的条件结果的组合,以及所有的入口点都至少执行一次。(注意“可能”二字,因为有些组合的情况难以生成。)(所有条件都有出现,但是只是出现,不像路径覆盖)
路径覆盖:每条路径至少执行一次。
任何一种覆盖方法都无法实现完全的测试。所以,在实际的测试用例设计过程中,根据需要将不同的覆盖方法组合起来使用,以实现最佳的测试用例设计 。
参考:https://www.jianshu.com/p/3152765ec902 (没错,这博客比我看的书讲的还详细)
判定覆盖和条件覆盖的区别:https://blog.csdn.net/u012441545/article/details/52229473 (我刚开始不知道区别,后来看了这个才懂)
junit简单用法: https://www.jianshu.com/p/a3fa5d208c93
练习
package top.sehnsucht.cover;
import lombok.Data;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/22
* ~~(^_^)~~
*/
@Data
public class Person {
private String sex;
private String name;
private int age;
private boolean healthy;
private boolean hasSafety;
public Person(String sex, String name, int age, boolean healthy, boolean hasSafety) {
this.sex = sex;
this.name = name;
this.age = age;
this.healthy = healthy;
this.hasSafety = hasSafety;
}
public Person(String sex, String name, int age) {
this.sex = sex;
this.name = name;
this.age = age;
}
public boolean isHealthy() {
return healthy;
}
public boolean hasSafety() {
return hasSafety;
}
}
package top.sehnsucht.cover;
import lombok.Data;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/22
* ~~(^_^)~~
*/
@Data
public class Safety {
private Person person;
private String type;
private int num = 1;
}
package top.sehnsucht.cover;
import org.junit.jupiter.api.Test;
import java.util.Scanner;
import java.util.TreeMap;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/22
* ~~(^_^)~~
*/
public class StatementCover {
public static Safety generateSafety(Person person) {
if (person.getSex().equals("男") && person.getAge() < 59 && person.getAge() > 16
|| person.getSex().equals("女") && person.getAge() < 54 && person.getAge() > 16) {
if (person.isHealthy() || person.hasSafety()) {
Safety safety = new Safety();
System.out.println("你可以选择A,B1,B2类型,数量为" + safety.getNum() );
safety.setType("A");
safety.setPerson(person);
System.out.println("投保成功");
return safety;
} else {
System.out.println("很遗憾,您只能选择C类型,即将自动帮您创建");
Safety safety = new Safety();
safety.setType("C");
safety.setPerson(person);
System.out.println("投保成功");
return new Safety();
}
}
System.out.println("投保不成功");
return null;
}
}
最后的测试部分
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import top.sehnsucht.cover.Person;
import top.sehnsucht.cover.Safety;
import top.sehnsucht.cover.StatementCover;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/9/25
* ~~(^_^)~~
*/
public class CoverTest {
@Test
public void statementCoverage1() {
Person person1 = new Person("男", "case1", 20, true, true);
Safety safety = StatementCover.generateSafety(person1);
Assertions.assertNotNull(safety);
System.out.println(safety);
}
@Test
public void statementCoverage2() {
Person person1 = new Person("男", "case2", 20, false, false);
Safety safety = StatementCover.generateSafety(person1);
Assertions.assertNotNull(safety);
System.out.println(safety);
}
@Test
public void DetermineCoverage3() {
Person person1 = new Person("男", "case3", 20, true, true);
Safety safety = StatementCover.generateSafety(person1);
Assertions.assertNotNull(safety);
System.out.println(safety);
}
@Test
public void DetermineCoverage4() {
Person person1 = new Person("男", "case4", 15);
Safety safety = StatementCover.generateSafety(person1);
Assertions.assertNull(safety);
}
@Test
public void DetermineCoverage5() {
Person person1 = new Person("男", "case5", 20, true, false);
Safety safety = StatementCover.generateSafety(person1);
Assertions.assertNotNull(safety);
System.out.println(safety);
}
}