gpt4 book ai didi

java - TestNG 中的多级设置/拆卸?

转载 作者:行者123 更新时间:2023-11-29 04:00:21 26 4
gpt4 key购买 nike

在我的 Selenium 框架中使用 TestNG 时,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 方法之间强制执行运行顺序,以便 postTestCase() 始终最后运行,而不管 seleniumTearDown 是否运行?

最佳答案

您的模型看起来有点虚,设置和拆卸应该不会失败。尽管他们可能会空手而归。如; “尝试建立数据库连接,但不可用,所以什么都不做”然后在拆除时,您应该在尝试关闭连接之前检查它们是否是连接。

否则,如果您想维护当前的模型,您可以使用某种手动检查而不是注释( boolean 值或单例类都可以)。

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

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

关于java - TestNG 中的多级设置/拆卸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3971487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com