基类@BeforeClass 配置初始化,初始化driver,安装启动app等,基类@AfterClass 退出前清理环境
public class Junit5Demo_Fixtures {
@BeforeAll
static void beforeAll() {
System.out.println("Father BeforeAll 执行了!");
}
@BeforeEach
void beforEach() {
System.out.println("Father BeforeEach 执行了!");
}
@AfterEach
void afterEach() {
System.out.println("Father AfterEach 执行了!");
}
@AfterAll
static void afterAll() {
System.out.println("Father AfterAll 执行了!");
}
@Test
void testMethod01() {
System.out.println("Father testMethod01 执行了!");
}
@Test
void testMethod02() {
System.out.println("Father testMethod0102 执行了!");
}
}
测试执行结果
public class Junit5Demo_Fixtures1 extends Junit5Demo_Fixtures {
@BeforeAll
static void childBeforeAll() {
System.out.println("child BeforeAll 执行了!");
}
@AfterAll
static void childAfterAll() {
System.out.println("child AfterAll 执行了!");
}
@BeforeEach
void childBeforEach() {
System.out.println("child BeforEach 执行了!");
}
@AfterEach
void childAfterEach() {
System.out.println("child AfterEach 执行了!");
}
@Test
void childTestMethod01() {
System.out.println("child TestMethod01 执行了!");
}
@Test
void childTestMethod02() {
System.out.println("child TestMethod02 执行了!");
}
}
@Test // 错误后依然继续执行
public void addTest() {
int resultPass = Calculator.add(4,2);
int resultFail = Calculator.add(4,2);
int result = Calculator.add(4,2);
System.out.println("add:" + result);
assertAll(("计算结果:"),
()->assertEquals(6,resultPass),
()->assertEquals(7,resultFail),
()->assertEquals(8,result));
}
public class LoginAndBuy {
//存放数据的变量datamap
private static HashMap<String,Object> datamap=new HashMap<String,Object>();
@Test
void Login(){
datamap.put("login","success");
System.out.println("用户登录状态为"+datamap.get("login"));
}
@Nested
@DisplayName("第二个执行")
class pay{
@Test
void paytest(){
if (StringUtils.isNotEmpty((CharSequence) datamap.get("buy"))){
System.out.println("正在支付...");
System.out.println(datamap.get("buy"));
}else {
System.out.println("尚未购买???????????");
}
}
}
@Nested //使用nested 进行业务流测试
@DisplayName("第一次执行")
class Buy{
@Test
void buyTest(){
// 判断用户是否登录成功
if (datamap.get("login").equals("success")){
System.out.println("用户登录成功可以购买");
datamap.put("buy","ok");
System.out.println("giao"+datamap.get("buy"));
}else {
System.out.println("用户未登录成功,请先登录");
}
}
}
执行结果:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章