簡體   English   中英

TestNG中有多個setUp / tearDown級別?

[英]Multiple levels of setUp/tearDown in TestNG?

在將TestNG用於我的Selenium框架時,setUp方法相對復雜。 有多個點可以打破,我想將它分成不同的步驟。

理想情況下,它看起來像這樣:

// Does some DB stuff, logs some messages
@BeforeMethod(alwaysRun = true)
preTestCase


// Instantiates and opens up Selenium
@BeforeMethod(dependsOnMethods = {"preTestCase"})
seleniumSetup


// Closes Selenium only if it was properly setup
@AfterMethod(dependsOnMethods = {"seleniumSetup"})
seleniumTearDown


// All other test case teardown, error logging
@AfterMethod(alwaysRun=true)
postTestCase

我想避免的是由於數據庫問題導致preTestCase失敗,然后由於seleniumTearDown嘗試關閉不存在的實例而導致二次失敗。 在這種情況下,只應運行postTestCase。 我收到此錯誤:seleniumTearDown()不允許依賴public void seleniumSetUp(org.testng.ITestContext)。 這是不允許/不好的設計? 如何在兩個tearDown方法之間強制執行run-order,以便postTestCase()始終最后運行,無論是否運行seleniumTearDown?

你的模型看起來有點不干凈,安裝和拆解不應該失敗。 雖然他們可能沒有。 如在; “嘗試進行數據庫連接,無法使用,所以沒有做任何事情”然后在拆除時你應該在嘗試關閉之前檢查它們是否是連接。

否則,如果您想維護當前模型,可以使用某種手動檢查而不是注釋(布爾值或單例類可以工作)。

In Setup:
if(dbGetConnected()) {
....
} else {
  dbisntconnected = true;
}

In tearDown:
if(!dbisntconnected) {
    dbClose();
}

您看到的錯誤是因為您試圖讓@AfterMethod依賴於@BeforeMethod,這沒有意義。 您可以讓配置方法相互依賴,但它們必須是所有相同的類型(例如,所有@AfterMethod或所有@BeforeMethod)。

至於你的另一個問題,Valchris給出的解決方案就是我推薦的。 如果您知道您的測試或配置是脆弱的,但它們不應該中斷測試運行,請自行捕獲異常,以便TestNG永遠不會看到它。

暫無
暫無

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

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