10

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.

4 Answers 4

13

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."); } 
Sign up to request clarification or add additional context in comments.

Comments

6

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 

3 Comments

Yes, your partially right. Only in the 'Test' or the 'BeforeMethod' annotated method. Within any other 'Configuration' method, it fails. I put together an entire project that tests all the variations and you can fork it and try the stuff yourself: djangofan.github.io/testng-custom-report-example
What is "other configuration" method?
In TestNG, a configuration method is one annotated with a Before or After annotation. So "other configuration method" means one annotated with BeforeClass, BeforeTest, BeforeSuite, or any of the After annotations.
0

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...

Comments

-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.

3 Comments

My question was asking for a "true skip" , not a disable, so that the skipped report still shows on my report.
This reminds me why I need to write a new Reporter/Runner. Enabled=false should not "not be reported"
Op was asking for skipping during runtime.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.