How to stop TestNG executioner when exception occu

2019-08-16 14:45发布

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(); 

标签: testng
2条回答
等我变得足够好
2楼-- · 2019-08-16 15:02

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-08-16 15:11

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.

查看更多
登录 后发表回答