简体   繁体   中英

Detect skipped test in @afterMethod with testng

I'm trying to detect skipped test in my @AfterMethod for reporting purpose.

I can catch failed or passed test but it doesn't seems to enter it when a test is skipped.

My tests :

@Test 
public void method1 () {
    Assert.fail("created failure");
}
@Test (dependsOnMethods = {"method1"})
public void method2 () {}

My AfterMethod :

@AfterMethod
protected void afterMethod(ITestResult result){
    switch (result.getStatus()) {
        case ITestResult.FAILURE:
            ...
            break;

        case ITestResult.SKIP:
            ...
            break;

        case ITestResult.SUCCESS:
            ...
            break;
    }

for example here i only retrieve the failure but the skip doesn't pass in the after method

Any idea on how to do it ?

Thank you !

I found a way of doing it by Implementing the ITestListener class you can find an example there :

https://github.com/khmarbaise/testng-example/blob/41861115eb0ea1d98eed97fcfeb7ff30e93e0925/src/test/java/com/soebes/testing/testng/IntegrationTestListener.java

basically what you do is creating a class that will catch all your ITestResult even the skipped one and redefine inside it what you want to do like that :

public class IntegrationTestListener implements ITestListener {
   public void onTestSkipped(ITestResult result) {
       // do your stuff here
   } 
}

and to use it in your test do it like that :

@Listeners ({IntegrationTestListener.class})
public class NotAScenario  extends BasicScenario {

    @Test
    public void test1() {
    }
}

For the @AfterMethod, when we know that the test has definitely run (thus not ITestResult.STARTED), there is a simple solution (tested and it works):

@AfterMethod(alwaysRun = true )
public void doStuff(ITestResult result){

  Integer status = result.getStatus();

  if(!status.equals(ITestResult.SUCCESS)  || !status.equals(ITestResult.FAILURE) ) { 
           // do stuff
   }    
}

Much cleaner for some cases where you don't want to implement all of the methods in ITestListener

In testng , if a test case is skipped, then @AfterMethod would not be called, as the test case has not been executed. @AfterMethod will get executed only when the test case is executed.

Hope this helps.

kartik is correct, u can not get it through after method, but in report u will find the skipped test case.

but if u want to get the skipped test cases, u can add soft dependacies here like : @Test(alwaysRun = true, dependsOnMethods = { "method" })

This creates a soft dependency, ie the annotated test should be executed even if the tests it depends on failed/threw an exception and it will invoke in after method.

You can capture skip tests by doing the following:

    public void afterMethod(ITestResult result) 
    throws Exception {
     if (result.getStatus() == ITestResult.SKIP) {
          //Some code
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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