Order of execution when I running tests in parallel

19 views
Skip to first unread message

MCD dotCom

unread,
Oct 23, 2022, 12:51:19 PM10/23/22
to testng-users
Hello,

ClassA:

public class ClassA {
   
    @Test(priority=1)
    public void test1() {
        System.out.println("Class A test 1");
    }
   
    @Test(priority=2)
    public void test2() {
        System.out.println("Class A test 2");
    }

}

ClassB:

public class ClassB {
   
    @Test(priority=1)
    public void test1() {
        System.out.println("Class B test 1");
    }
   
    @Test(priority=2)
    public void test2() {
        System.out.println("Class B test 2");
    }

}

ClassC:

public class ClassC {
   
    @Test(priority=1)
    public void test1() {
        System.out.println("Class C test 1");
    }
   
    @Test(priority=2)
    public void test2() {
        System.out.println("Class C test 2");
    }

}

XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">
  <test thread-count="5" name="Test">
    <classes>
      <class name="TestNGPriority.ClassA"/>
      <class name="TestNGPriority.ClassB"/>
    </classes>
  </test> <!-- Test -->
 
    <test thread-count="5" name="SECOND TST">
    <classes>
      <class name="TestNGPriority.ClassC"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

OUTPUT

Class A test 1
Class C test 1
Class B test 1
Class C test 2
Class A test 2
Class B test 2

I was expecting ClassA methods to execute before ClassB methods.
How can I run ClassA methods (test1 follow by test2) before I run ClassB(test1 follow by test2)?

Thank you!

MCD dotCom

unread,
Oct 23, 2022, 1:39:39 PM10/23/22
to testng-users
I added the listener and it worked. Thanks!


public class OrderListener implements IMethodInterceptor {

    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        List<IMethodInstance> result = new ArrayList<IMethodInstance>();
        for (IMethodInstance method : methods) {
            Test testMethod = method.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class);
            result.add(method);
        }
        return result;

    }

}


XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests">

    <listeners>
        <listener class-name="listeners.OrderListener" />
    </listeners>

   
  <test thread-count="5" name="Test">
    <classes>
      <class name="TestNGPriority.ClassA"/>
      <class name="TestNGPriority.ClassB"/>
    </classes>
  </test> <!-- Test -->
 
    <test thread-count="5" name="SECOND TST">
    <classes>
      <class name="TestNGPriority.ClassC"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

OUTPUT
Class A test 1
Class C test 1
Class A test 2
Class C test 2
Class B test 1
Class B test 2

Reply all
Reply to author
Forward
0 new messages