1.常用注解
执行顺序:
BeforeAll BeforEach > 测试用例 > AfterEach BeforEach > 测试用例 > AfterEach BeAfterAll
代码演示
public class DemoTest {
@BeforeAll
static void beforeAll(){
System.out.println("开始测试。。。");
}
@AfterAll
static void afterAll(){
System.out.println("测试结束。。。");
}
@BeforeEach
void setup(){
System.out.println("打开浏览器");
}
@AfterEach
void teardown(){
System.out.println("关闭浏览器");
}
@Test
void queryCaseTest(){
System.out.println("query dates to ...");
assertEquals(2,1+1);
}
@Test
@DisplayName("登录测试")
void loginCaseTest(){
System.out.println("login to website");
assertEquals(1,1);
}
}
2.断言
代码演示:
public class JunitLearnTest {
@Test
void assertEqualsDemo1(){
assertEquals("hello","hello");
}
@Test
void assertAllDemo1(){
assertAll("响应断言",
()->assertEquals(2,1),
()->assertEquals(2,2),
()->assertEquals(3,3)
);
}
@Test
void assertAllDemo2() throws InterruptedException{
ArrayList<Executable> executables = new ArrayList<>();
executables.add(()->assertEquals(2,2));
executables.add(()->assertEquals(2,2));
assertAll("执行流错误信息",executables.stream());
}
3.参数化
public class JunitLearnTest {
@ParameterizedTest
// 数据参数化
// @CsvSource({"张三,14","李四,23","王五,34"})
// 文件参数化
@CsvFileSource(resources="/testData.csv",delimiterString = ",")
void paramDemo(String name , Integer age){
System.out.println(name+"的年龄是"+age+"岁");
}