Priority in TestNG with multiple classes

后端 未结 6 982
再見小時候
再見小時候 2020-11-27 20:15

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

@Test( priority = 1 )
public void testA1() {
             


        
相关标签:
6条回答
  • 2020-11-27 20:48

    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

    0 讨论(0)
  • 2020-11-27 20:49

    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>
    

    0 讨论(0)
  • 2020-11-27 20:55

    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

    0 讨论(0)
  • 2020-11-27 20:58

    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.

    0 讨论(0)
  • 2020-11-27 21:01

    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 讨论(0)
  • 2020-11-27 21:06

    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 讨论(0)
提交回复
热议问题