简体   繁体   English

仅在我的@Test通过时运行@AfterMethod,而不是在@Test失败时运行

[英]Run @AfterMethod only when my @Test is passed, not if @Test fails

My afterMethod consists of this: 我的afterMethod由以下afterMethod组成:

@AfterMethod(groups = { "Regression" })
public void afterMethod() {
    // Setting driver used to false as this test case is pass
    driverUsed.put("supportDriver", false);
    System.out.println("Test case is pass");
}

where I am putting the driver false 我把驱动程序false

But I want to run this afterMethod only when my @Test is passed, not when @test fails. 但是我希望只在我的@Test通过时才运行afterMethod ,而不是@test失败时。

To quote TestNG's documentation : 引用TestNG的文档

Any @AfterMethod method can declare a parameter of type ITestResult , which will reflect the result of the test method that was just run. 任何@AfterMethod方法都可以声明一个ITestResult类型的参数,它将反映刚刚运行的测试方法的结果。

You can use this parameter to check if the test succeeded or not: 您可以使用此参数来检查测试是否成功:

@AfterMethod(groups = { "Regression" })
public void afterMethod(ITestResult result) {
   if (result.getStatus() == ITestResult.SUCCESS) {
        // Setting driver used to false as this test case is pass
        driverUsed.put("supportDriver", false);
        System.out.println("Test case is pass");
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM