Reputation: 681
I am trying to learn about skipping a test in TestNG by throwing SkipException as it helps you skip the tests which are not ready. But the exception information being displayed is messing up the report. Is there any way to avoid that? The code is:
import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipTest {
@Test(expectedExceptions = SkipException.class)
public void skip() throws Exception{
throw new SkipException("Skipping");
System.out.println("skip executed");
}
Excepting the exception and throwing it in the method declaring are not helping.
Upvotes: 0
Views: 456
Reputation: 117
If you would like to skip tests that are not ready, use @Test(enabled=false) annotation.
Upvotes: 0
Reputation: 14736
If you would like to mark a @Test
method as skipped, and still would not like to see the exception information in the reports, then you should not be throwing the TestSkipException
.
Here's how you do this (You would need to leverage TestNG listeners)
The test class looks like below
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(SkipMarker.class)
public class SampleTestClass {
@Test
public void passMethod() {}
@Test
public void skipMethod() {
// Adding an attribute to the current Test Method's result object to signal to the
// TestNG listener (SkipMarker), that this method needs to be marked as skipped.
Reporter.getCurrentTestResult().setAttribute("shouldfail", true);
}
}
Here's how the TestNG listener looks like
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
public class SkipMarker implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
// Look for the signalling attribute from the test method's result object
Object value = testResult.getAttribute("shouldfail");
if (value == null) {
// If the attribute was not found, dont proceed further.
return;
}
// attribute was found. So override the test status to failure.
testResult.setStatus(ITestResult.FAILURE);
}
}
Upvotes: 1
Reputation: 58774
System.out.println("skip executed");
was unreachable, if you fix your test as below there's no exception information being displayed
import org.testng.SkipException;
import org.testng.annotations.Test;
public class SkipTest {
@Test(expectedExceptions = SkipException.class)
public void skip() {
throw new SkipException("Skipping");
}
}
Upvotes: 1