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?

NobodyNada
  • 7,529
  • 6
  • 44
  • 51
peetya
  • 3,578
  • 2
  • 16
  • 30

6 Answers6

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

Sachin Francis
  • 1,071
  • 9
  • 9
  • 1
    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. – peetya Oct 30 '14 at 08:28
  • I don't have an xml... how to set this in Java? – sebnukem Aug 11 '16 at 17:14
  • 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); – Androdos Aug 19 '16 at 06:33
  • 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 1 – Aditi Jul 06 '20 at 15:36
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.

Marinos An
  • 9,481
  • 6
  • 63
  • 96
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.

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

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>

rweris
  • 9
  • 1
  • 1
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

Thangalakshmi
  • 33
  • 1
  • 5