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:
- What's the best way to force TestNG stop execution when exception occurs by Test or configuration method?
- 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();
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, whosebeforeInvocation
method wil also fire for configuration methods. If you want to cancel the rest of your test executions, throw a SkipException everytimebeforeInvocation
is called.In my case, this successfully cancelled all further processing without making any assumptions about the test structure.
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.