Junit5
- selenium是自动化测试框架(写自动化测试用例)
- Junit单元测试框架(管理写好的测试用例)
注解:@Test,@Disable ,@BeforeAll,@AfterAll,@BeforeEach,@AfteEach
@Test注解
表示当前的方法是一个测试用例。
public class test1 {
@Test
void test1(){
WebDriver webDriver=new ChromeDriver();
webDriver.get("https://www.baidu.com");
webDriver.findElement(By.cssSelector("#su")).click();
}
}
@Disabled
暂不执行被标注的方法
@BeforeAll 和 @AfterAll
@BeforeAll
static void SetUp(){
//这里放初始化
System.out.println("先执行beforeAll");
}
@AfterAll
static void TearDown(){
//这里放关闭资源
System.out.println("先执行后执行AfterAll");
}
@BeforeEach和@AfteEach
BeforeAll:在所有的测试用例运行之前跑对应的方法
AfterAll:在所有的测试用例运行完之后,跑对应的方法
BeforeEach:每个用例之前运行
AfteEach:每个用例执行完后运行
参数化
导入依赖:中央仓库
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
@ParameterizedTest和 @ValueSource(ints = {1,2,3})(单参数)
@ParameterizedTest
@ValueSource(ints = {1,2,3})
void Test04(int num){
System.out.println(num);
}
@ParameterizedTest和@CsvFileSource(resources = “test01.csv”)(csv获取参数(传递同种类型))
文件名必须.csv
@ParameterizedTest
@CsvFileSource(resources = "test01.csv")
void test05(String name1){
System.out.println(name1);
}
@ParameterizedTest和@MethodSource(“Generator”)(多参数)
Generator是方法
@CsvSource可以填入多个参数,参数中也可传入空值
public static Stream<Arguments> Generator() {
return Stream.of(Arguments.arguments(1,"张三"),Arguments.arguments(2,"王五"));
}
@ParameterizedTest
@MethodSource("Generator")
void Test07(int num ,String name){
System.out.println(num+":"+name);
}
控制测试用例执行顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) 设定顺序执行
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class test2 {
@Order(1)
@Test
void A(){
System.out.println("A");`在这里插入代码片`
}
@Order(3)
@Test
void B(){
System.out.println("B");
}
@Order(2)
@Test
void C(){
System.out.println("C");
}
}
@TestMethodOrder(MethodOrderer.Random.class) 随机执行
@TestMethodOrder(MethodOrderer.Random.class)
public class test2 {
@Order(1)
@Test
void A(){
}
@Order(3)
@Test
void B(){
}
@Order(2)
@Test
void C(){
}
}
断言,Assertions
断言相等, Assertions.assertEquals
@ParameterizedTest
@ValueSource(ints = {1})
void Test01(int num){
Assertions.assertEquals(1,num);
}
断言不相等,Assertions.assertNotEquals
@ParameterizedTest
@ValueSource(ints = {1})
void Test01(int num){
Assertions.assertNotEquals(1,num);
}
断言为空,Assertions.assertNull();
@Test
void Test02(){
String str=null;
Assertions.assertNull(str);
}
断言不为空, Assertions.assertNotNull()
@Test
void Test02(){
String str=null;
Assertions.assertNotNull(str);
}
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>
</dependency>
将测试类加入套件中
@Suite
@SelectClasses({test1.class,test2.class})
public class RunSutite {
}
通过调用包中测试
@Suite
@SelectPackages(value = {"test01","test"})
public class RunSutite {
}