【问题标题】:How do I catch exceptions if thrown in @BeforeMethod or @BeforeClass without using a TestNGListener如果在 @BeforeMethod 或 @BeforeClass 中抛出异常而不使用 TestNGListener,我如何捕获异常
【发布时间】:2016-08-03 12:06:39
【问题描述】:

我有一个针对特定功能的测试类,并有多层测试类,它们被视为 BaseClass,其中发生驱动程序初始化和测试启动(我在这里避免使用 Listener)。在 BaseClass 中定义了一个 AfterMethod,它注销并使用异常和堆栈跟踪捕获结果并记录到数据库,如果测试失败我能够捕获异常,如果 BeforeMethod 或 BeforeClass 失败,则不会捕获异常,所有我请参阅显示为已跳过的测试状态。这是我在 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
}

如何捕获BeforeMethodBeforeClass 中引发的异常

【问题讨论】:

    标签: java selenium testng


    【解决方案1】:

    您可以使用具有适当方法的侦听器:IConfigurationListener#onConfigurationFailure(ITestResult)

    查看the documentation 了解有关侦听器的更多详细信息。

    【讨论】:

      【解决方案2】:

      如果您不想通过 Listeners 访问它,那么为什么不这样访问它们呢?

      来自ITestResult 对象,该对象通过您的@AfterMethod 注释方法:

      • 首先通过result.getTestContext().getFailedConfigurations().getAllMethods()获取所有失败的配置方法

      • 然后您通过调用org.testng.ITestNGMethod#isBeforeClassConfiguration 来遍历ITestNGMethod 的上述集合以仅过滤掉@BeforeClass 故障

      【讨论】:

        猜你喜欢
        • 2016-02-17
        • 2021-07-24
        • 2013-06-24
        • 2020-10-03
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 2018-03-23
        • 2015-02-13
        相关资源
        最近更新 更多