Not able to execute TestNG tests in order wise

90 views
Skip to first unread message

adithya vardhanreddy

unread,
Nov 13, 2017, 3:03:18 AM11/13/17
to testng-users

I want to execute my TestNG Class tests in order wise how they have written from to to bottom. To achive this right now i am using priority for each test in one testNG class.

Sample TestNg Class:
FirsttestNG.class assume this class is having 3 tests as shown below
@test(priority=1)
public void test1()
{
}
@test(priority=2)
public void test2()
{
}
@test(priority=3)
public void test3()
{
}

SecondTestNG.class assume this class also having 3 tests
@test(priority=1)
public void secondtest1()
{
}
@test(priority=2)
public void secondtest2()
{
}
@test(priority=3)
public void secondtest3()
{
}

When i execute above mentioned classes alone from testNg xml file the out put will be as shown below.
for FirstTestNgClass out put : first test1 method will execute then test2 follwed by test3.
for SecondtestNg class also execution sequence will be same.

Now the problem is when i execute both FirstTestNG and SecondTestNG Classes at a time form testNg xml then execution sequence will be
Out Put
test1
secondtest1
test2
secondtest2
test3
secondtest3

but i want as shown below:
out Put:
test1
test2
test3
secondtest1
secondtest2
secondtest3

can we achive this ?

Pavankumar Vedantham

unread,
Nov 13, 2017, 4:57:38 AM11/13/17
to testng...@googlegroups.com
We can achieve it by calling the test classes from different test tag in same xml file like below also put preserve-order =true at suite level:

<test name="Test 1">

  <classes>

  <class name="FirstTestNG"></class>

   </classes>

 </test>

  <test name="Test 2">

  <classes>

   <class name="SecondTestNG"></class>

   </classes>

  </test>


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

nikhi...@agrostar.in

unread,
Nov 21, 2017, 4:47:07 AM11/21/17
to testng-users
Hello,

I am also facing the same issue.


My testng XML:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="CRM Test">
  <test name="UI Test" group-by-instances="true">
    <classes>
      <class name="test.java.com.testsuite.Class1"/>
      <class name="test.java.com.testsuite.Class2"/>
      <class name="test.java.com.testsuite.Class3"/>
    </classes>
  </test> <!-- UI Test -->
</suite> <!-- CRM Test -->


First Class:


public class Class1{
     @Test(priority = 1, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test1(LinkedHashMap<String, String> data) {

     }

     @Test(priority = 2, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test2(LinkedHashMap<String, String> data) {

     }

     @Test(priority =3, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test3(LinkedHashMap<String, String> data) {

     }

}


Second Class:

public class Class2{
     @Test(priority = 1, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test1(LinkedHashMap<String, String> data) {

     }

     @Test(priority = 2, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test2(LinkedHashMap<String, String> data) {

     }

     @Test(priority =3, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test3(LinkedHashMap<String, String> data) {

     }

}


Third Class:


public class Class3{
     @Test(priority = 1, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test1(LinkedHashMap<String, String> data) {

     }

     @Test(priority = 2, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test2(LinkedHashMap<String, String> data) {

     }

     @Test(priority =3, dataProvider = "getData", dataProviderClass = DataUtil.class)
public void Test3(LinkedHashMap<String, String> data) {

     }

}


I am generating XML runtime as user says I have to run only Class1 and Class2 then XML contains only Class1 and Class2.

Output:

Class1: Test1
Class2: Test1
Class1: Test2
Class2: Test2
Class1: Test3
Class2: Test3


Expected:

Class1: Test1
Class1: Test2
Class1: Test3

Class2: Test1
Class2: Test2
Class2: Test3


I am using latest testng 6.11





On Monday, November 13, 2017 at 3:27:38 PM UTC+5:30, Pavankumar Vedantham wrote:
We can achieve it by calling the test classes from different test tag in same xml file like below also put prese@rve-order =true at suite level:
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

Julien Herr

unread,
Nov 21, 2017, 10:53:01 AM11/21/17
to testng-users
Hi,

It is the expected order:
priority is used to order tests in <test> and tests with the same priority will be executed together.

You may use another ordering feature like dependsOnMethods which is class scoped.

Julien

nikhi...@agrostar.in

unread,
Nov 22, 2017, 12:22:27 AM11/22/17
to testng-users
Thanks Julien Herr.

nikhi...@agrostar.in

unread,
Dec 19, 2017, 1:33:33 AM12/19/17
to testng-users
Solution for the code:


static List<Class<? extends ITestNGListener>> listenerClasses = new ArrayList<>();


public static List<XmlSuite> genarateXmlFile() {
List<XmlTest> tests = new ArrayList<XmlTest>();
List<XmlSuite> suites = new ArrayList<XmlSuite>();

XmlSuite suite = new XmlSuite();
suite.setName(Constant.BaseTestHelperConstant.SUITENAME);
suite.setPreserveOrder(true);

for (int i = 0; i < Common.BaseTestHelperClass.sheetsName.size(); i++) {
XmlTest test = new XmlTest(suite);
List<XmlClass> classes = new ArrayList<XmlClass>();
test.setName(Common.BaseTestHelperClass.sheetsName.get(i));
classes.add(new XmlClass(Constant.BaseTestHelperConstant.TESTSUITEPATH + Common.BaseTestHelperClass.sheetsName.get(i)));
test.setXmlClasses(classes);
tests.add(test);
}

listenerClasses.add(main.java.com.agrostar.utility.DataProviderAdder.class);

suites.add(suite);

System.out.println("Printing TestNG Suite Xml");
System.out.println(suite.toXml());

return suites;

}

Where class values are dynamic.

Radek eM

unread,
May 22, 2018, 4:30:58 AM5/22/18
to testng-users
Hi, 

Was it also true in 6.9.x versions? I was testing a case with the "preserve-order=true" set for a <test>, and tests were grouped according to the order defined in XML but inside each test class test methods were "sorted" by priority. Am I right?
Reply all
Reply to author
Forward
0 new messages