How do I use TestNG throw new SkipException() effectively? Does anyone have an example?
I tried throwing this exception at the start of a test method but it blows up the teardown, setup, methods, etc. , and has collateral damage by causing a few (not all) of the subsequent tests to be skipped also, and shows a bunch of garbage on the TestNG HTML report.
I use TestNG to run my unit tests and I already know how to use an option to the @Test annotation to disable a test. I would like my test to show up as "existent" on my report but without counting it in the net result. In other words, it would be nice if there was a @Test annotation option to "skip" a test. This is so that I can mark tests as ignored sortof without having the test disappear from the list of all tests.
Is "SkipException" required to be thrown in @BeforeXXX before the @Test is ran? That might explain the wierdness I am seeing.
The root exception for special skip handling. In case a @Test or @Configuration throws this exception the method will be considered a skip or a failure according to the return of isSkip() . Users may provide extensions to this mechanism by extending this class.
1 Answer. Show activity on this post. try-catch would be the best solution, because any exceptions generated out of the methods should be handled by that method instead of the testng framework. @AfterMethod public void clearData() throws Exception { try{ // do something.... }
Compile the test case using javac. Now, run the testng. xml, which will run the test case defined in <test> tag. Verify the output.
Groups in Groups Step 1: Open the Eclipse. Step 2: We create a java project named as "Groups_in_Groups". Step 3: Now we create a testng. xml file where we configure the above class.
I'm using TestNG 6.8.1.
I have a few @Test
methods from which I throw SkipException
, and I don't see any weirdness. It seems to work just as expected.
@Test
public void testAddCategories() throws Exception {
if (SupportedDbType.HSQL.equals(dbType)) {
throw new SkipException("Using HSQL will fail this test. aborting...");
}
...
}
Maven output:
Results :
Tests run: 85, Failures: 0, Errors: 0, Skipped: 2
Yes, my suspicion was correct. Throwing the exception within @Test doesn't work, and neither did throwing it in @BeforeTest, while I am using parallel by classes. If you do that, the exception will break the test setup and your TestNG report will show exceptions within all of the related @Configuration methods and may even cause subsequent tests to fail without being skipped.
But, when I throw it within @BeforeMethod, it works perfectly. Glad I was able to figure it out. The documentation of the class suggests it will work in any of the @Configuration annotated methods, but something about what I am doing didn't allow me to do that.
@BeforeMethod
public void beforeMethod() {
throw new SkipException("Testing skip.");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With