Nirav Sanghavi
Nirav Sanghavi

Reputation: 1

How to stop TestNG executioner when exception occures?

I am running TestNG programmatically. I have a use case where I need to stop the executioner when any exception occurred by any tests. My questions are:

  1. What's the best way to force TestNG stop execution when exception occurs by Test or configuration method?
  2. Capture the exception in console when executing programmatically?

Here is the code snippet for executing test runner:

TestListenerAdapter tla = new TestListenerAdapter();
IMethodInterceptor im = new  TestMethodInterceptor();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { TestClass1.class, TestClass2.class});
testng.addListener(tla);
testng.setMethodInterceptor(im);
testng.run(); 

Upvotes: 0

Views: 3334

Answers (2)

user3792852
user3792852

Reputation: 355

Using an IHookable, as described in the previous answer, is a good way to simulate a complete test suite cancellation by skipping all following methods. However, it is not notified for (and therefore does not cancel) configuration methods (e.g. methods annotated with @BeforeClass and @BeforeMethod), and often this is where the real processing is going on.

To deal with this, you can replace the IHookable by an IInvokedMethodListener, whose beforeInvocation method wil also fire for configuration methods. If you want to cancel the rest of your test executions, throw a SkipException everytime beforeInvocation is called.

In my case, this successfully cancelled all further processing without making any assumptions about the test structure.

Upvotes: 1

niharika_neo
niharika_neo

Reputation: 8531

As far as I know, there is no direct provision for halting execution.

You can probably have 2 listeners added using the addListener calls. One implementing ITestListener and another implementing IHookable, examples of both you would get on testng's site. ITestListener would give you a method onTestFailure(). Here you can set a value which would indicate that there has been a failure. In the IHookable implementation, you can check for this value and only then invoke the method, else skip it.

Exception printing can also be handled in the onTestFailure method.

Upvotes: 1

Related Questions