Reputation: 29669
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.
Upvotes: 10
Views: 18208
Reputation: 31
While using DataProvider empty test using Apache POI create seperate check @BeforeTest we can skip the data base is empty or null in that scenario we can use this skiptest with row check is empty using boolean true check then skipped that expection do not go to entire check its having 1000 input check rather its skip that data provider null...
Upvotes: 0
Reputation: 8314
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
Upvotes: 6
Reputation: 29669
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.");
}
Upvotes: 13
Reputation: 648
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.
Upvotes: -2