Priority in TestNG with multiple classes
Asked Answered
G

7

19

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?

Goldsberry answered 29/10, 2014 at 13:56 Comment(0)
M
22

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

Misinform answered 29/10, 2014 at 16:4 Comment(4)
Thank you, it works fine for me in my example code too :) But do you know maybe how to pass this parameter to testng.xml programatically? I tried "setGroupByInstances(true)" but it didn't work.Goldsberry
I don't have an xml... how to set this in Java?Apomorphine
To peetya: I think it's important to set setGroupByInstances(true) in right place ... as in example, it is in "Test" element, so: XmlTest test = new XmlTest(suite); test.setGroupByInstances(true);Compliant
Hi This is not working for me, Please help. In class A , I have 2 @Test priority 1 and priority 2. On the other hand In class B, I have 3 Test priority 1, 2 and 3 . while running through testng.xml it executes Class A, Priority 1 then jump to Class B Priority 1Frequent
L
1

You can just provide @Test(testName="test1") / @Test(testName="test2") at the top of each class, and the priorities will be automatically grouped per class. Of course you keep the existing annotations.

Lesleelesley answered 22/6, 2017 at 13:15 Comment(4)
Can you give testng.xml for it ? One exampleGaselier
@Gaselier I haven't used testng.xml at allLesleelesley
But I want to run with testng xml. Is that possible ? I have 3 class and I want to give sequence.Gaselier
@Gaselier So I guess the answer has been provided by Sachin FrancisLesleelesley
L
1

The most correct way is to use dependsOnMethods. Priority levels are global for test (don't mix with test-methods which are annotated with @Test). In other words: when testng runs test (from <test> tag) it groups methods by priorities and then run it. In your case both testA1 and testB1 have priority=1, so will be executed at the beginning.

Lackey answered 6/12, 2017 at 17:16 Comment(0)
U
0

you should change the priority on B test to be like this

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

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

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

and no changes for XML because it runs as priority

Ulloa answered 10/7, 2017 at 3:18 Comment(0)
S
0

In my case I've separated classes into different tests in testng.xml file and priorities worked as in earlier versions used to.

<suite name="Suite1" verbose="1">
<test name="TVS_AUTO_TESTS 1">
    <classes>
        <class name="TVS_auto_tests.CheckLoginTests"/>
    </classes>
</test>
<test name="TVS_AUTO_TESTS 2">
    <classes>
        <class name="TVS_auto_tests.PageNavigationTests"/>
    </classes>
</test>

Smoot answered 12/3, 2019 at 13:45 Comment(0)
R
0

Group the test methods of first class and put dependsOnGroups in test methods of 2nd class.So execution will be proper as per the expectation shown below

ClassOne is as follows

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

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

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

ClassTwo is as follows

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

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

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

And Finally testng.xml is

<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="TestCMD.ClassOne"/>
      <class name="TestCMD.ClassTwo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

It gives the same output as per priority given in both the classes and order also

Ralf answered 20/11, 2019 at 13:48 Comment(0)
N
0
but is there a way to run both the class in parallel but it should not be like below out put 

testA1
testB1
testA2
testB2
testA3
testB3

instead i need to run my test a and test b at the same time not to consider the based on priority because currently my test is running like 
testA1
testB1
testA2
testB2
testA3
testB3
and the variable are overlapping and tests are failing 
 
Northwester answered 16/7 at 6:10 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Underhill

© 2022 - 2024 — McMap. All rights reserved.