[英]How do I catch exceptions if thrown in @BeforeMethod or @BeforeClass without using a TestNGListener
I have a test class each for a specific functionality and have layers of Test Classes that are considered as BaseClass wherein the driver initialization and test kickoff happens (I avoided using Listener here). 我有一个针对特定功能的测试类,并具有被视为BaseClass的测试类层,其中发生了驱动程序初始化和测试启动(我在这里避免使用Listener)。 Defined an AfterMethod in BaseClass which logs out and also captures the results with the exceptions and stacktrace and logs to a database, if the test fails I am able to capture the exceptions, the exceptions are not caught if the BeforeMethod or BeforeClass fails, all I see is test status which is shown as SKIPPED.
在BaseClass中定义了一个AfterMethod,该方法可以注销并捕获带有异常和stacktrace的结果,并记录到数据库中,如果测试失败,我可以捕获异常,如果BeforeMethod或BeforeClass失败,则不捕获异常,所有看到的是测试状态,显示为“跳过”。 Here is the piece of code that I use in AfterMethod:
这是我在AfterMethod中使用的代码:
public void afterMethodBase(ITestResult result) {
logger.debug("TestName : " + result.getTestName());
logger.info("Method Name : " + result.getMethod().getMethodName());
ResultLogger.processResult(result);
Throwable exception = itr.getThrowable(); // this is null when status is skipped
}
How to catch the exceptions raised in BeforeMethod
or BeforeClass
如何捕获
BeforeMethod
或BeforeClass
引发的异常
You can use a listener with its appropriate method: IConfigurationListener#onConfigurationFailure(ITestResult)
您可以使用侦听器及其适当的方法:
IConfigurationListener#onConfigurationFailure(ITestResult)
Check the documentation for more details about listeners. 查看文档以获取有关侦听器的更多详细信息。
If accessing it via the Listeners is not something that you want to do, then why not access them like this ? 如果您不想通过侦听器访问它,那么为什么不这样访问它们呢?
From the ITestResult
object that get through your @AfterMethod
annotated method: 从通过您的
@AfterMethod
注释方法的ITestResult
对象中:
First get hold of all the failed configuration methods via result.getTestContext().getFailedConfigurations().getAllMethods()
首先通过
result.getTestContext().getFailedConfigurations().getAllMethods()
掌握所有失败的配置方法
Then you iterate through the above collection of ITestNGMethod
to filter out only @BeforeClass
failures by calling org.testng.ITestNGMethod#isBeforeClassConfiguration
然后,通过调用
org.testng.ITestNGMethod#isBeforeClassConfiguration
遍历上述ITestNGMethod
集合,以仅过滤出@BeforeClass
故障。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.