2

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 !

1
  • try implementing a TestListener, shall help. Commented May 4, 2016 at 16:46

6 Answers 6

1

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() { } } 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

This worked for me:

@AfterMethod public void afterMethod(ITestContext context) { System.out.println("Skips: " + context.getSkippedTests().size()); } 

Comments

0

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.

1 Comment

-1. @AfterMethod(alwaysRun = true) and then yes the method will run. There are plenty of cases when you want @AfterMethod to run to do some cleanup. It is like the Java finally block
0

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, i.e. the annotated test should be executed even if the tests it depends on failed/threw an exception and it will invoke in after method.

Comments

0

You can capture skip tests by doing the following:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.