Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use TestNG SkipException?

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.

like image 476
djangofan Avatar asked Feb 06 '14 00:02

djangofan


People also ask

What is SkipException Java?

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.

How do you rerun skip test cases in TestNG?

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

How do you run a TestNG test?

Compile the test case using javac. Now, run the testng. xml, which will run the test case defined in <test> tag. Verify the output.

How do I run an xml group in TestNG?

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.


2 Answers

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
like image 73
Jeff Fairley Avatar answered Oct 14 '22 17:10

Jeff Fairley


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.");
}
like image 32
djangofan Avatar answered Oct 14 '22 17:10

djangofan