简体   繁体   English

TestNG的课程运行混乱(在测试类别之间跳过)

[英]TestNG is Running Classes Out of Order (Skipping Between Classes of Tests)

I am having an issue with TestNG Selenium Webdriver 2.0, and Java. 我在使用TestNG Selenium Webdriver 2.0和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. 每个类都从初始化一些对该类中所有测试全局的变量开始,包括对另一个初始化Webdriver的类的调用。 Next is a @BeforeClass, and next are my @Test tests. 接下来是@BeforeClass,接下来是我的@Test测试。 I am running the classes from a testng.xml file. 我正在从testng.xml文件运行类。

On debugging an issue lately I found that at runtime, testNG does the following: 最近在调试问题时,我发现在运行时,testNG会执行以下操作:

  1. Initialize the global variables and webdriver in class1 初始化class1中的全局变量和webdriver
  2. Then skips over to the top of class2 and does the same 然后跳到class2的顶部并执行相同的操作
  3. Then skips back to class1 @BeforeClass 然后跳回到class1 @BeforeClass
  4. Then runs the tests in class1 5 then skips back to class2 然后在class1 5中运行测试,然后跳回到class2

@BeforeClass and finishes from there... @BeforeClass并从那里结束...

Why would testNG behave this way. 为什么testNG会这样表现。 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. 我已经尝试了逐步操作,但是testNG是编译后的代码,因此我无法弄清楚为什么在上述步骤2之前它不能以class1结尾。 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. 在class1中的webdriver之后立即初始化class2中的webdriver产生了一个奇怪的问题,即在不关闭class2的驱动程序的情况下,我无法在class1的末尾执行driver.close()。 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. 并且由于class2已经具有其全局变量并且已初始化其webdriver,因此当class1测试完成后testNG最终移回到class2时,将忽略其webdriver初始化。 Also at runtime I can see one webbrowser open up to one path (for class1) then go to another path (for class2). 同样在运行时,我可以看到一个Web浏览器打开了一个路径(对于class1),然后转到了另一个路径(对于class2)。 It's just not right. 只是不对。 Any ideas why testNG is running in such an order? 有什么想法为什么testNG以这样的顺序运行?

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). 事实证明,导致testNG在类之间跳过的问题是我在类级别而不是在方法(@Test)中初始化变量,类等。 You can declare NULL objects but may not initialize them to any values anywhere except within a method. 您可以声明NULL对象,但除了方法内的任何地方,都不能将它们初始化为任何值。 This includes the webdriver! 这包括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. 因此,基本上是一种设置方法,需要对类定义范围的任何say变量都需要在类中首先运行。 Hope this helps somebody save some time. 希望这可以帮助某人节省时间。 Thanks- JR. 谢谢-JR

you can prioritize that runs in sequence in TESTNG:

you can define in annotations simply,


@Test(priority=0) // this will run first test
public void methdone()
{
}

@Test(priority=1)
public void methodtwo()
{
}
@Test(priority=2) // this will run as third test,
public void methodthird()
{
}
hope it helps !!

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

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