1

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?

0

4 Answers 4

0

you can put the methods in ClassExample1 on a group, and then use dependsOnGroup, for example:

public class classExample1 {
@Test(groups = GROUP_EXAMPLE)
public void testEx1() {
     System.out.println("testEx1");
}
}

public class classExample2 {
@Test
public void testEx2(dependsOnGroups = GROUP_EXAMPLE) {
     System.out.println("testEx2");
}
}

This way, testEx1 will always execute before testEx2. And you can use priority to granulate your results even more.

0

You can run multiple test classes by using xml file (you can create xml file by right click on project)

https://howtodoinjava.com/testng/testng-executing-parallel-tests/ This link will help you to resolve this issue

0

For running all test methods of one class first and then for the other classes, testng.xml file structure needs to be changed. You need to specify test methods from each class in the order of their execution.

Without this change, the XML file will run as per priority, e.g. testA1() and then testB1().

Please find the XML file required to achieve tests class wise :

<suite name="REGRESSION_TEST_SET" thread-count="1" parallel="tests" >
<test  name="AUTOMATION" group-by-instances="true">
 <classes>
        <class name="ClassA" />
          <methods>
                <include name="testA1"/>
                <include name="testA2"/>
                <include name="testA3"/>
          </methods>
       </class>  
        
        <class name="ClassB" />
          <methods>
                <include name="testB1"/>
                <include name="testB2"/>
                <include name="testB3"/>
          </methods>
       </class>  
    </classes>
</test>
0

In TestNG, the order of test methods within a class is determined by the priority attribute specified for each test method. However, when one has multiple classes and want to execute the tests within each class separately, one can use the preserve-order attribute in your testng.xml file.

To achieve the desired result of running the tests from one class at a time before moving on to the next class, you can modify your testng.xml file as follows:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" preserve-order="true">
  <test name="Test A">
    <classes>
      <class name="your.package.name.Program">
        <methods>
          <include name="testA1"/>
          <include name="testA2"/>
          <include name="testA3"/>
        </methods>
      </class>
    </classes>
  </test>
  
  <test name="Test B">
    <classes>
      <class name="your.package.name.Program">
        <methods>
          <include name="testB1"/>
          <include name="testB2"/>
          <include name="testB3"/>
        </methods>
      </class>
    </classes>
  </test>
</suite>

In the above testng.xml configuration, two separate tags are used, each representing a different class (in this case, it refers to the same Program class). The section is used within each to specify the individual test methods to include for execution.

By including only the specific test methods from each class in the section, you ensure that only those methods are executed. Therefore, in the first tag ("Test A"), only testA1, testA2, and testA3 will be executed in the specified order. Similarly, in the second tag ("Test B"), only testB1, testB2, and testB3 will be executed in the specified order.

When you run your tests using this modified testng.xml file, you should get the expected output: testA1 testA2 testA3 testB1 testB2 testB3

2
  • Welcome to Stack Overflow! I see you've been a member here a while, but this looks like your first post. It appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). Please be aware that posting of AI-generated content is banned here. If you used an AI tool to assist with any answer, I would encourage you to delete it. Jun 22, 2023 at 20:45
  • Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation. If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. Jun 22, 2023 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.