簡體   English   中英

如果不使用TestNGListener拋出@BeforeMethod或@BeforeClass,如何捕獲異常

[英]How do I catch exceptions if thrown in @BeforeMethod or @BeforeClass without using a TestNGListener

我有一個針對特定功能的測試類,並具有被視為BaseClass的測試類層,其中發生了驅動程序初始化和測試啟動(我在這里避免使用Listener)。 在BaseClass中定義了一個AfterMethod,該方法可以注銷並捕獲帶有異常和stacktrace的結果,並記錄到數據庫中,如果測試失敗,我可以捕獲異常,如果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引發的異常

您可以使用偵聽器及其適當的方法: IConfigurationListener#onConfigurationFailure(ITestResult)

查看文檔以獲取有關偵聽器的更多詳細信息。

如果您不想通過偵聽器訪問它,那么為什么不這樣訪問它們呢?

從通過您的@AfterMethod注釋方法的ITestResult對象中:

  • 首先通過result.getTestContext().getFailedConfigurations().getAllMethods()掌握所有失敗的配置方法

  • 然后,通過調用org.testng.ITestNGMethod#isBeforeClassConfiguration遍歷上述ITestNGMethod集合,以僅過濾出@BeforeClass故障。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM