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.
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.");
}
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
For skipping test case from @Test annotation option you can use 'enable=false' attribute with @Test annotation as below
@Test(enable=false)
This will skip the test case without running it. but other tests, setup and teardown will run without any issue.