【软件测试学习day7】Junit5
Junit 是单元测试框架,本期掌握 Junit5 的基础用法。
1. 注解
首先引入 Junit 依赖:
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.9.1</version><scope>test</scope>
</dependency>
1.1 @Test
通过 @Test 这个注解就是代表了,被该注解所修饰的代码为一块测试代码。
@Testvoid Test01(){System.out.println("第一个单元测试");}
1.2 @Disabled
当我不想让某个测试用例运行时,可使用 @Disabled 注解。
@Testvoid Test01(){System.out.println("第一个单元测试");}@Disabledvoid Test02(){System.out.println("第二个测试");}
1.3 @BeforeAll和@AfterAll
@BeforeAll 和 @AfterAll 是对一个类进行注解的。
@BeforAll 类似于 SpringBoot 中的前置通知,在所有用例前进行运行。
@AfterAll 类似于 SpringBoot 中的后置通知是在所有测试用例后运行。
@Testvoid Test01(){System.out.println("第一个单元测试");}@Disabledvoid Test02(){System.out.println("第二个测试");}@BeforeAllstatic void SetUp(){System.out.println("BeforeAll注解");}@AfterAllstatic void TearDown(){System.out.println("AfterAll注解");}
两者之间运用场景,例如 JDBC 链接数据库,配置连接信息以及初始化则用 @BeforAll 注解,关闭连接则使用 @AfterAll 注解。
1.4 @BeforeEach和@AfterEach
@BeforeEach 在每个测试方法前都执行一次。
@AfterEach 在每个测试方法后都执行一次。
@Testvoid Test01(){System.out.println("第一个单元测试");}@Testvoid Test02(){System.out.println("第二个测试");}@BeforeAllstatic void SetUp(){System.out.println("BeforeAll注解");}@AfterAllstatic void TearDown(){System.out.println("AfterAll注解");}@BeforeEachvoid BeforeEachTest() {System.out.println("BeforeEach注解");}@AfterEachvoid AfterEachTest() {System.out.println("AfterEach注解");}
2. 参数化
引入依赖
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-params</artifactId><version>5.9.1</version><scope>test</scope></dependency>
2.1 单参数
@ParameterizedTest:参数化测试。
@ValueSource:数据来源,使用时只能表示一种你类型如 ints 代表整型,chars 代表字符型。
@ParameterizedTest@ValueSource(ints = {1,2,3})void init(int num) {System.out.println(num);}
2.2 多参数
@CsvSource 注解里面的参数可被多个类型识别。
@ParameterizedTest@CsvSource({"1,2,3"})void Test03(String x,int y,int z){System.out.println(x);System.out.println(y);System.out.println(z);}
2.3 CSV获取参数
@CsvFileSource 注解可访问 .csv 文件。
@ParameterizedTest@CsvFileSource(resources = "test.csv")void Test02(String name){System.out.println(name);}
3. 测试用例执行顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) 注解声明当前类可被指定顺序。
@Order 注解用来标注执行顺序。
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {@Order(2)@Testvoid A(){System.out.println("AAA");}@Order(1)@Testvoid B(){System.out.println("BBB");}@Testvoid C(){System.out.println("CCC");}
}
4. 断言
if else 语句的替代,可用:
- Assertions.assertEquals(1,num); - 满足 num 为 1 则通过。
- Assertions.assertNotEquals(1,num); - 满足 num 不为 1 则通过。
- Assertions.assertNull(str); - 满足 str 为空则通过。
@ParameterizedTest@ValueSource(ints = {1})void Test(int num){Assertions.assertEquals(1,num);// 是1则通过Assertions.assertNotEquals(1,num);// 不是1则通过String str =null;Assertions.assertNull(str);// 是null则通过}
5 . 测试套件
有测试类1:
public class JunitTest {@Order(2)@Testvoid A(){System.out.println("AAA");}@Order(1)@Testvoid B(){System.out.println("BBB");}@Testvoid C(){System.out.println("CCC");}
}
测试类2:
public class JunitTest02 {@Testvoid init(){System.out.println("XXX");}
}
使用 @Sutie 注解 和 @SelectClasses 注解可将上述两类合并执行。
@Suite
@SelectClasses({JunitTest.class,JunitTest02.class})
public class RunSuite {
}
此外,当两个类属于不同包时,也能通过 @SelectPackages 注解来实现合并执行。
@Suite
@SelectPackages(value = {"Test01","Test02"})
public class RunSuite {
}
6. 所有依赖
本篇文章的所有依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>my_test</artifactId><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></dependency><!-- https://mvnrepository.com/artifact/commons-io/commons-io --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.11.0</version></dependency><!-- 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><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-params</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><dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-suite</artifactId><version>1.9.1</version><scope>test</scope></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></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>