testng

Priority in TestNG with multiple classes


I'm facing with the following problem: I created two classes which include @Tests with priority attribute:

@Test( priority = 1 )
public void testA1() {
    System.out.println("testA1");
}

@Test( priority = 2 )
public void testA2() {
    System.out.println("testA2");
}

@Test( priority = 3 )
public void testA3() {
    System.out.println("testA3");
}

... and ...

@Test( priority = 1 )
public void testB1() {
    System.out.println("testB1");
}

@Test( priority = 2 )
public void testB2() {
    System.out.println("testB2");
}

@Test( priority = 3 )
public void testB3() {
    System.out.println("testB3");
}

I put both classes under one test in testng.xml but when I run the test, it will order my @Tests based on the priorities from both classes:

testA1
testB1
testA2
testB2
testA3
testB3

I'm expecting the following result:

testA1
testA2
testA3
testB1
testB2
testB3

My question is that how can I prevent to order my @Tests based on both classes and run @Tests only from one class at the same time?


Solution

  • In your suite xml use group-by-instances="true"

    Sample, where TestClass1 and TestClass2 has the same content as yours

    <suite thread-count="2" verbose="10" name="testSuite" parallel="tests">
    <test verbose="2" name="MytestCase" group-by-instances="true">
        <classes>
            <class name="com.crazytests.dataproviderissue.TestClass1" />
            <class name="com.crazytests.dataproviderissue.TestClass2" />
        </classes>
    </test>
    </suite> 
    

    I get the output

    testA1

    testA2

    testA3

    testB1

    testB2

    testB3