How to use TestNG SkipException?

How to use TestNG to effectively use the new SkipException () ? Does anyone have an example?

I tried to throw this exception at the beginning of the test method, but it blew up the gap, setup, methods, etc., and has collateral damage, also causing several (not all) subsequent tests to be skipped, and showing a bunch of garbage in the TestNG HTML report.

I use TestNG to run my unit tests, and I already know how to use the parameter for @Test annotation to disable the test. I would like my test to appear as "existing" in my report, but not counting it in the end result. In other words, it would be nice if there was an @Test annotation option to β€œskip” the test. This means that I can mark the tests as ignored by sortof without checking the test from the list of all tests.

Does a β€œSkipException” need to be thrown at @BeforeXXX before running @Test? This may explain the strangeness I see.

+8
exception unit-testing testng testing
source share
3 answers

Yes, my suspicion was correct. Throwing an exception from @Test does not work, and it also did not throw it at @BeforeTest, while I use parallel classes. If you do this, the exception will violate the test setup, and your TestNG report will show exceptions in all the @Configuration related methods and may even lead to subsequent failures without gaps.

But when I throw it at @BeforeMethod, it works fine. Glad I could figure it out. The class documentation suggests that it will work in any of the @Configuration annotated methods, but something about what I am doing did not allow me to do this.

@BeforeMethod public void beforeMethod() { throw new SkipException("Testing skip."); } 
+9
source share

I am using TestNG 6.8.1.

I have several @Test methods from which I throw SkipException , and I do not see any oddities. It seems to work 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 
+4
source share

To skip the test case from the @Test annotation option, you can use the "enable = false" attribute with the @Test annotation, as shown below

 @Test(enable=false) 

This will skip the test case without running it. but other tests, setup and shutdown will be performed without any problems.

-one
source share

All Articles