How to run multiple test classes in TestNG?

TestNGRest AssuredDynamic Programming

testng.xml has a format as <classes> where we define what all test classes should be executed. Users can mention n number of classes in testing.xml that require executing. In this article, we are going to discuss how to execute more than one class using a single testing.xml.

Here, we will have two classes with multiple test methods, and we will see how testng.xml is configured to run both the classes - NewTestngClass and OrderofTestExecutionInTestNG.

Approach/Algorithm to solve this problem

  • Setp 1 − Create two TestNG classes - NewTestngClass and OrderofTestExecutionInTestNG.

  • Setp 2 − Write two different @Test method in both the classes - NewTestngClass and OrderofTestExecutionInTestNG.

  • Setp 3 − Now create the testNG.xml as given below.

  • Setp 4 − Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

The following code shows how to run multiple classes −

src/ NewTestngClass.java

import org.testng.annotations.Test;
public class NewTestngClass {
   @Test
   public void testCase1() {
      System.out.println("in test case 1 of NewTestngClass");
   }
   @Test
   public void testCase2() {
      System.out.println("in test case 2 of NewTestngClass");
   }
}

src/OrderofTestExecutionInTestNG.java

import org.testng.annotations.Test;
public class OrderofTestExecutionInTestNG {
   // test case 1
   @Test
   public void testCase3() {
      System.out.println("in test case 3 of OrderofTestExecutionInTestNG");
   }
   // test case 2
   @Test
   public void testCase4() {
      System.out.println("in test case 4 of OrderofTestExecutionInTestNG");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass" />
         <class name = "OrderofTestExecutionInTestNG" />
      </classes>
   </test>
</suite>

Output

in test case 1 of NewTestngClass
in test case 2 of NewTestngClass
in test case 3 of OrderofTestExecutionInTestNG
in test case 4 of OrderofTestExecutionInTestNG
===============================================
Suite1
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
raja
Published on 12-Jan-2022 06:54:25
Advertisements