目录
表示当前的这个方法是一个测试用例
要引入依赖(中央仓库-->junit-->JUnit Jupiter API » 5.9.1)
- <!-- 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>
- @Test
- void Test01(){
- System.out.println("这是Junit中Test01");
- }
忽略不运行这个测试用例
- @Disabled
- void Test02(){
- System.out.println("这是Junit中Test01");
- }
方法前要加static
@BeforeAll:在运行其他所有测试用例之前运行 只运行一次 一般存放连接,开启
@AfterAll:在运行其他所有测试用例之后运行 只运行一次 一般存放关闭
- @BeforeAll
- static void SetUp(){
- System.out.println("这是BeforeAll的语句");
- }
-
- @AfterAll
- static void TearDown(){
- System.out.println("这是AfterAll的语句");
- }
@BeforeEach 在运行每一个测试用例之前运行 有几个测试用例运行几次
@AfterEach 在运行每一个测试用例之后运行 有几个测试用例运行几次
- @BeforeEach
- void SetUp(){
- System.out.println("这是BeforeAll的语句");
- }
-
- @AfterEach
- void TearDown(){
- System.out.println("这是AfterAll的语句");
- }
引入依赖 中央仓库-->Parameterized-->JUnit Jupiter Params » 5.9.1
- <!-- 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>
@ParameterizedTest
@ValueSource(类型+s={,,,})
- @ParameterizedTest
- @ValueSource(ints={1,2,3})
- void Test04(int num){
- System.out.println(num);
- }
-
- @ParameterizedTest
- @ValueSource(strings={"1","2","3"})
- void Test05(String num){
- System.out.println(num);
- }
@ParameterizedTest
@CsvSource({,,,})
- @ParameterizedTest
- @CsvSource({"1,2,3"})
- void Test06(String x,String y,String z){
- System.out.println(x);
- System.out.println(y);
- System.out.println(z);
- }
@ParameterizedTest
@CsvFileSource(resources=" 文件名.csv")
- @ParameterizedTest //运行几次取决于csv有几行
- @CsvFileSource(resources = "test01.csv")//这个文件必须以csv结尾
- void Test06(String name){
- System.out.println(name);
- }
test01.csv
- 张三1. 李四1. 王五1
- 张三2. 李四2. 王五2
- 张三3. 李四3. 王五3
@ParameterizedTest
@MethodSource("Generator")
测试数据:
public static Stream
Genertor(){ return Stream.of(Arguments.arguments(),
Arguments.arguments(),
Arguments.arguments(),...
);
}
- @ParameterizedTest
- @MethodSource("Generator")
- //接收
- void Test07(int num,String name){
- System.out.println(num+" : "+name);
- }
- //测试数据
- public static Stream<Arguments> Generator(){
- return Stream.of(Arguments.arguments(1,"张三"),
- Arguments.arguments(2,"李四"),
- Arguments.arguments(3,"王五")
- );
- }
更改代码顺序,不改变运行顺序
执行顺序固定:Junit有自己固定的执行顺序
类前加注解@TestMethodOrder(MethodOrder.OrderAnnotation.class)
每个用例前加@Order(i) 在第i个执行,i从1开始
@TestMethodOrder(MethodOrder.Random.class)
- //断言相等
- Assertions.assertEquals(1,num);
- //断言不相等
- Assertions.assertNotEquals(2,num);
- String str=null;
- //断言为空
- Assertions.assertNull(str);
- //断言不为空
- String str1="null1";
- Assertions.assertNotNull(str1);
引入依赖
- <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>
- @Suite
- //通过class测试用例执行
- @SelectClasses({JunitTest.class,JunitTest01.class,JunitTest02.class})//规定执行顺序
- public class RunSuite {
- }
- @Suite
- @SelectPackages(value ={"Package01","Package02"})
- public class RunSuite {
- }