Monday, September 23, 2019

Post#112.How to Execute Multiple Classes From Different Packages Using testng.xml File


How to Execute Multiple Classes from Different Packages:
We have already seen example of how to configure testng.xml file to run single/multiple classes of same package. 
In this post we will see how to execute multiple classes from different packages.
First of all let’s create new packages and classes as described in below steps.

Step 1: Create package = "TestNGPack1" and under this package Create classes as Program1.java and Program2.java
Step 2: Create package = "TestNGPack2" and under this package Create classes as Program3.java and Program4.java
Once done with above steps your Project structure will look like this.



Program1.java

package TestNGPack1;

import org.testng.annotations.Test;

public class Program1  {
              //@Test annotation describes this method as a test method
               @Test
                public void TC001() {
                  System.out.println("====TC001 Executed====");
                }
}

Program2.java

package TestNGPack1;
import org.testng.annotations.Test;
public class Program2 {
              //@Test annotation describes this method as a test method
               @Test
                public void TC002() {
                              System.out.println("====TC002 Executed====");
                }
              }

Program3.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program3  {
              //@Test annotation describes this method as a test method
               @Test
                public void TC003() {
                              System.out.println("====TC003 Executed====");
                }
}

Program4.java

package TestNGPack2;
import org.testng.annotations.Test;
public class Program4 {
              //@Test annotation describes this method as a test method
               @Test
                public void TC004() {
                              System.out.println("====TC004 Executed====");
                }
}

Scenario: 1

How to execute all the classes of all the packages in one test?

Ans: You need to configure your testng.xml file as bellow.
Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, all classes are included in single test (Test One).

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   <class name="TestNGPack1.Program2" />
   <class name="TestNGPack2.Program3" />
   <class name="TestNGPack2.Program4" />
   </classes>
 </test>
 </suite>


Once execution completed the execution result reports will looks like this.


Scenario: 2

How to execute selected classes (Let’s say Program1 and Program3) of 2 different packages in separate test?

Ans: You need to configure your testng.xml file as bellow.
Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, all classes are included in single test (Test One).

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   </classes>
 </test>
 <test name="Test two" >
  <classes>
   <class name="TestNGPack2.Program3" />
   </classes>
 </test>
 </suite>


Once execution completed the execution result reports will looks like this.




No comments:

Post a Comment