简体   繁体   English

TestNG中有多个setUp / tearDown级别?

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

In using TestNG for my Selenium framework, the setUp method is relatively complex. 在将TestNG用于我的Selenium框架时,setUp方法相对复杂。 There are multiple points that it can break and I would like to split it into separate steps. 有多个点可以打破,我想将它分成不同的步骤。

Ideally it would look something like this: 理想情况下,它看起来像这样:

// 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

What I want to avoid is failing in preTestCase due to a DB issue and then getting a secondary failure due to seleniumTearDown trying to close a non-existent instance. 我想避免的是由于数据库问题导致preTestCase失败,然后由于seleniumTearDown尝试关闭不存在的实例而导致二次失败。 In that situation, only postTestCase should be run. 在这种情况下,只应运行postTestCase。 I am getting this error: seleniumTearDown() is not allowed to depend on public void seleniumSetUp(org.testng.ITestContext). 我收到此错误:seleniumTearDown()不允许依赖public void seleniumSetUp(org.testng.ITestContext)。 Is this not allowed / bad design? 这是不允许/不好的设计? How do I enforce run-order between the two tearDown methods so that postTestCase() is always run last, regardless of if seleniumTearDown is run? 如何在两个tearDown方法之间强制执行run-order,以便postTestCase()始终最后运行,无论是否运行seleniumTearDown?

Your model seems a little washy, Setup and Teardown shouldn't fail. 你的模型看起来有点不干净,安装和拆解不应该失败。 Although they could possibly no-op. 虽然他们可能没有。 As in; 如在; "Attempted to make a db connection, wasn't available so did nothing instead" then in tear down you should check if their is a connection before attempting to close it. “尝试进行数据库连接,无法使用,所以没有做任何事情”然后在拆除时你应该在尝试关闭之前检查它们是否是连接。

Otherwise if you want to maintain your current model you could use some sort of manual check instead of the annotation (a boolean or a singleton class would work). 否则,如果您想维护当前模型,可以使用某种手动检查而不是注释(布尔值或单例类可以工作)。

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

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

The error you are seeing is because you are trying to have an @AfterMethod depend on a @BeforeMethod, which doesn't make sense. 您看到的错误是因为您试图让@AfterMethod依赖于@BeforeMethod,这没有意义。 You can have configuration methods depend on each other, but they need to be all of the same type (eg all @AfterMethod or all @BeforeMethod). 您可以让配置方法相互依赖,但它们必须是所有相同的类型(例如,所有@AfterMethod或所有@BeforeMethod)。

As for your other problem, the solution given by Valchris is what I recommend. 至于你的另一个问题,Valchris给出的解决方案就是我推荐的。 If you know your tests or configurations are fragile but they shouldn't interrupt the test run, catch the exception yourself so that TestNG will never see it. 如果您知道您的测试或配置是脆弱的,但它们不应该中断测试运行,请自行捕获异常,以便TestNG永远不会看到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM