简体   繁体   中英

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. 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. Initialize the global variables and webdriver in class1
  2. Then skips over to the top of class2 and does the same
  3. Then skips back to class1 @BeforeClass
  4. Then runs the tests in class1 5 then skips back to class2

@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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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