Reputation: 491
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 !
Upvotes: 2
Views: 3336
Reputation: 11
This worked for me:
@AfterMethod
public void afterMethod(ITestContext context) {
System.out.println("Skips: " + context.getSkippedTests().size());
}
Upvotes: 1
Reputation: 2248
You can capture skip tests by doing the following:
public void afterMethod(ITestResult result)
throws Exception {
if (result.getStatus() == ITestResult.SKIP) {
//Some code
}
Upvotes: 0
Reputation: 11911
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
Upvotes: 1
Reputation: 491
I found a way of doing it by Implementing the ITestListener class you can find an example there :
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() {
}
}
Upvotes: 1
Reputation: 3004
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.
Upvotes: 0
Reputation: 761
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.
Upvotes: 0