Saturday, September 28, 2019

Post#115.How to Include-Exclude Selected Test package Using testng.xml File


How to Include/Exclude Selected Test packages using testng.xml File
In this Post I will explain you, how you can include/exclude selected test packages for execution.
First of all configure your Project Structure 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

First of all configure your Project Structure 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====");
              }
             @Test
              public void TC002() {
                System.out.println("====TC002 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 TC003() {
                         System.out.println("====TC003 Executed====");
              }
             @Test
              public void TC004() {
                         System.out.println("====TC004 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 TC005() {
                         System.out.println("====TC005 Executed====");
              }
             @Test
              public void TC006() {
                         System.out.println("====TC006 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 TC007() {
                         System.out.println("====TC007 Executed====");
              }
             @Test
              public void TC008() {
                         System.out.println("====TC008 Executed====");
              }
}
Scenario:
I want to execute only “TestNGPack2” Package.
To perform above scenario, I have to configure my testng.xml file as bellow.

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

Note: we can use wildcard(.*) with package tag to run all packages but then immediately we will use include to run specific package from all the packages.

Now execute this testng.xml file and verify the result report.


If you see in above test result of TestNG suite, only classes and methods of "TestNGPack2" package are executed. Remaining package “TestNGPack1” is not executed.

Configuring testng.xml file to exclude specific package from execution
To exclude specific package from execution, you need to configure your testng.xml file as bellow. 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite One" >
 <test name="Test One" >
            <packages>
        <package name=".*">
           <exclude name="TestNGPack2" />
        </package>
     </packages>
  </test>
 </suite>
This way we can include or exclude full package from execution.



No comments:

Post a Comment