Java 使用testng在@afterMethod中检测跳过的测试
我试图在@AfterMethod中检测跳过的测试,以便报告 我可以捕获失败或通过的测试,但当跳过测试时,它似乎不会进入测试 我的测试:Java 使用testng在@afterMethod中检测跳过的测试,java,testng,appium,Java,Testng,Appium,我试图在@AfterMethod中检测跳过的测试,以便报告 我可以捕获失败或通过的测试,但当跳过测试时,它似乎不会进入测试 我的测试: @Test public void method1 () { Assert.fail("created failure"); } @Test (dependsOnMethods = {"method1"}) public void method2 () {} 我的方法: @AfterMethod protected void afterMethod(
@Test
public void method1 () {
Assert.fail("created failure");
}
@Test (dependsOnMethods = {"method1"})
public void method2 () {}
我的方法:
@AfterMethod
protected void afterMethod(ITestResult result){
switch (result.getStatus()) {
case ITestResult.FAILURE:
...
break;
case ITestResult.SKIP:
...
break;
case ITestResult.SUCCESS:
...
break;
}
例如,在这里,我只检索失败,但跳过不会传递after方法
你知道怎么做吗
谢谢大家! 在
testng
中,如果跳过测试用例,则不会调用@AfterMethod
,因为测试用例尚未执行@AfterMethod
将仅在执行测试用例时执行
希望这有帮助。kartik是正确的,您无法通过after方法完成它,但在报告中您将找到跳过的测试用例 但是如果你想得到跳过的测试用例,你可以在这里添加软依赖项,比如:@test(alwaysRun=true,dependsOnMethods={“method”})
这将创建一个软依赖项,即即使它所依赖的测试失败/抛出异常,并且它将在after方法中调用,也应执行带注释的测试。我通过实现ITestListener类找到了一种方法。您可以在那里找到一个示例: 基本上,您所做的是创建一个类,该类将捕获您的所有ITestResult,甚至是跳过的ITestResult,并在其中重新定义您希望执行的操作:
public class IntegrationTestListener implements ITestListener {
public void onTestSkipped(ITestResult result) {
// do your stuff here
}
}
@Listeners ({IntegrationTestListener.class})
public class NotAScenario extends BasicScenario {
@Test
public void test1() {
}
}
要在测试中使用它,请这样做:
public class IntegrationTestListener implements ITestListener {
public void onTestSkipped(ITestResult result) {
// do your stuff here
}
}
@Listeners ({IntegrationTestListener.class})
public class NotAScenario extends BasicScenario {
@Test
public void test1() {
}
}
对于@AfterMethod,当我们知道测试肯定已经运行(因此不是ITestResult.STARTED)时,有一个简单的解决方案(经过测试,它可以工作):
在某些情况下,如果不想实现
ITestListener
中的所有方法,则可以通过执行以下操作捕获跳过测试:
public void afterMethod(ITestResult result)
throws Exception {
if (result.getStatus() == ITestResult.SKIP) {
//Some code
}
尝试实现一个
TestListener
,会有所帮助@AfterMethod(alwaysRun=true)
然后是,该方法将运行。在很多情况下,您需要运行@AfterMethod来进行一些清理。它就像Javafinally
块