内容大纲:
常用的五个注解
测试用例顺序指定
参数化
测试套件
断言
1. 常用的五个注解
1.1 @Test
通常情况下,我们输入要写在main方法下,此时我想直接输出:
java
@Test
void Test01(){
System.out.println("================第一个测试用例");
}
1.2 @BeforeAll @AfterAll
BeforeALL在Test修饰的方法之前运行,AfterAll在之后运行
java
@Test
void Test01(){
System.out.println("================第一个测试用例");
}
@Test
void Test02(){
System.out.println("================第二个测试用例");
}
@BeforeAll
static void Start(){
System.out.println("=================开始测试===============");
}
@AfterAll
static void End(){
System.out.println("=================结束测试===============");
}

1.3 @BeforeEach @AfterEach
java
@Test
void Test01(){
System.out.println("================第一个测试用例");
}
@Test
void Test02(){
System.out.println("================第二个测试用例");
}
@BeforeEach
void StartV1(){
System.out.println("***开始执行***");}
@AfterEach
void EndV1(){
System.out.println("***结束执行***");
}

2. 测试用例顺序指定
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Order(2)
java
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Order(2)
@Test
void Test01(){
System.out.println("================第一个测试用例");
}
@Order(1)
@Test
void Test02(){
System.out.println("================第二个测试用例");
}

3. 参数化
1.1 单参数
java
/**
* 单参数
*/
@ParameterizedTest
@ValueSource(ints={1,2,3,4})
void Test03(int x){
System.out.println("接收的参数x:" + x);
}

单参数有他的弊端,只能接收一种类型的参数,对应的Test方法只能接收一个参数
1.2 多参数
java
/**
* 多参数
*/
@ParameterizedTest
@CsvSource({"'张三','28'"})
void Test04(String name,int age){
System.out.println(name + "今年" + age +"岁");
}

读取文件中的数据进行操作:
每组数据用逗号隔开
java
//多参数,读取文件中的数据,进行操作
@ParameterizedTest
@CsvFileSource(resources = "test01.csv")
void Test05(String name,int age){
System.out.println("name: " + name + ",age: "+age);
}

注意:
参数化对应的注解上如果写了@Test,测试用例就会执行两次,其中一次就会报错,因为Test和方法混合,没有找到参数,所以报错
1.3 通过方法生成参数
java
/**
* 通过方法生成参数
*/
@ParameterizedTest
@MethodSource("Generate")
void Test06(String name,int age){
System.out.println("name: " + name + ",age: "+age);
}
public static Stream<Arguments> Generate() {
return Stream.of(
Arguments.arguments("张山",12),
Arguments.arguments("lisi",23));
}

4. 测试套件
@Suite
4.1 通过class运行测试用例

java
@Suite
//通过class运行测序用例
@SelectClasses({JunitTest2.class,JunitTest.class})
public class RunTests {
}

4.2 通过包运行测试用例

java
@Suite
//通过包运行测试用例
@SelectPackages(value = {"example"})
public class RunTests {
}

5. 断言
断言相等/不相等/为空/不为空
java
/**
* 断言
*/
@Test
void Test07(){
int x = 10;
int y = 20;
Assertions.assertEquals(x,y);//判断断言是否相等
Assertions.assertNotEquals(x,y);//判断断言是否不相等
String temp = null;
Assertions.assertNull(temp);//判断断言是否为空
Assertions.assertNotNull(temp);//判断断言是否不为空
}
6. 依赖


java
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>1.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>