Saturday, September 28, 2019

Post#113.How to execute All Or Specific Packages Using testng.xml File

How to execute All Or Specific Packages
In testng.xml file, You can also configure to run specific package or all packages of a project.
Let’s consider that your Project Structure is as below.
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====");
              }
}
Configure testng.xml to run selected packages from all packages of “DemoTestNG” project in single test suite.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
            <packages>
                        <package name="TestNGPack2" />
   </packages>
 </test>
 </suite>

In above testng.xml code, <packages> tag is used to describe group of packages and <package> tag package is used to add specific package in our test suite. 
So when you execute above testng.xml file, it will run only targeted packages. Here I have given only one package. i.e TestNGPack2
You can add more packages according to your requirements.
Once execution completed the execution result reports will looks like this.

As you see, "TestNGPack1" package is not executed as shown in above report image.

Configure testng.xml to run all packages of project in single test suite
If you wants to run all packages of your project, you can configure your testng.xml using wildcard(.*) with package tag as bellow.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
            <packages>
                        <package name=".*" />
   </packages>
 </test>
 </suite>
Once execution completed the execution result reports will looks like this.


As you see in report, now all the packages are executed. This way, we can use wild card to include all the test packages in our test.

No comments:

Post a Comment