TestNG 中的多个级别的设置/拆卸?
在我的 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 是否运行?
In using TestNG for my Selenium framework, the setUp method is relatively complex. 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. In that situation, only postTestCase should be run. I am getting this error: seleniumTearDown() is not allowed to depend on 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的模型看起来有点单调,安装和拆卸不应失败。尽管他们可能不进行任何操作。如; “尝试建立数据库连接,但不可用,因此什么也不做”,然后在拆卸中,您应该在尝试关闭连接之前检查它们是否是连接。
否则,如果您想维护当前模型,您可以使用某种手动检查而不是注释(可以使用布尔值或单例类)。
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).
您看到的错误是因为您试图让 @AfterMethod 依赖于 @BeforeMethod,这是没有意义的。您可以让配置方法相互依赖,但它们必须全部属于同一类型(例如,全部@AfterMethod 或全部@BeforeMethod)。
至于你的其他问题,Valchris给出的解决方案是我推荐的。如果您知道您的测试或配置很脆弱,但它们不应该中断测试运行,请自己捕获异常,以便 TestNG 永远不会看到它。
The error you are seeing is because you are trying to have an @AfterMethod depend on a @BeforeMethod, which doesn't make sense. You can have configuration methods depend on each other, but they need to be all of the same type (e.g. all @AfterMethod or all @BeforeMethod).
As for your other problem, the solution given by Valchris is what I recommend. 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.