TestNG 乱序运行类(在测试类之间跳过) [英] TestNG is Running Classes Out of Order (Skipping Between Classes of Tests)

查看:33
本文介绍了TestNG 乱序运行类(在测试类之间跳过)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 TestNG Selenium Webdriver 2.0 和 Java 时遇到问题.我设置了断点并看到了最奇怪的行为.
我有几个包含测试组的类文件.每个类都从初始化类中所有测试的全局变量开始,包括对另一个类的调用,该类初始化 webdriver.接下来是@BeforeClass,接下来是我的@Test 测试.我正在运行 testng.xml 文件中的类.

最近在调试一个问题时,我发现在运行时,testNG 执行以下操作:

I am having an issue with TestNG Selenium Webdriver 2.0, and Java. I set breakpoints and saw the strangest behavior.
I have several class files containing groups of tests. Each class begins with initializing some variables global to all tests in the class, including a call to another class which initializes the webdriver. Next is a @BeforeClass, and next are my @Test tests. I am running the classes from a testng.xml file.

On debugging an issue lately I found that at runtime, testNG does the following:

  1. 初始化 class1 中的全局变量和 webdriver
  2. 然后跳到 class2 的顶部并执行相同的操作
  3. 然后跳回到 class1 @BeforeClass
  4. 然后在 class1 中运行测试 5 然后跳回到 class2

@BeforeClass 并从那里完成...

为什么 testNG 会这样.我试过单步执行,但 testNG 是编译代码,所以我无法弄清楚为什么它没有在上面的第 2 步之前以 class1 完成.在 class1 中的 webdriver 之后立即初始化 class2 中的 webdriver 会产生一个奇怪的问题,即我无法在 class1 结束时执行 driver.close() 而不关闭 class2 的驱动程序.并且由于class2已经有它的全局变量和它的webdriver初始化,当class1测试完成后testNG最终回到class2时,它的webdriver初始化被忽略了.同样在运行时,我可以看到一个 webbrowser 打开一个路径(对于 class1)然后转到另一个路径(对于 class2).只是不对.任何想法为什么 testNG 以这样的顺序运行?

@BeforeClass and finishes from there...

Why would testNG behave this way. I have tried stepping through but testNG is compiled code so I can't figure out why it does not finish with class1, before step 2 above. Initializing the webdriver in class2 right after the webdriver in class1 creates an odd problem that I cannot do a driver.close() at the end of class1 without closing the driver of class2. And since class2 has already had its global variables and its webdriver initialized, when testNG finally moves back to class2 after class1 tests are finished, its webdriver initialization is ignored. Also at runtime I can see one webbrowser open up to one path (for class1) then go to another path (for class2). It's just not right. Any ideas why testNG is running in such an order?

推荐答案

事实证明,导致 testNG 在类之间跳过的问题是我在类级别而不是在方法内初始化变量、类等 (@测试).您可以声明 NULL 对象,但不能将它们初始化为除方法内以外的任何地方的任何值.这包括网络驱动程序!所以基本上是一个设置方法,首先需要在类中运行任何需要作用域到类的变量.希望这可以帮助某人节省一些时间.谢谢 - JR.

It turns out that the problem causing testNG to skip between classes was that I was initializing variables, classes, etc at the class level and not within a method (@Test). You can declare NULL objects but may not initialize them to any values anywhere except within a method. This includes the webdriver! So basically a setup method, first one run in the class is needed for any say variables that will need to be scoped to the class. Hope this helps somebody save some time. Thanks- JR.

这篇关于TestNG 乱序运行类(在测试类之间跳过)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆