Include & Exclude Tests in testng.xml

How to Include/Exclude Selected Test Methods Using testng.xml File

In this Post I will explain you, how you can include/exclude selected test methods from 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

Each Class containing two methods as shown in below image.

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

TC001() method from TestNGPack1->Program1.java
TC003() method from TestNGPack1->Program2.java
TC005() method from TestNGPack2->Program3.java
TC007() method from TestNGPack2->Program4.java

How to do?

Configure testng.xml file to Include and run selected test methods from corresponding classes.
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" >
 	<classes>
        <class name="TestNGPack1.Program1" >
          <methods>
             <include name="TC001" />
          </methods>
          </class>
          <class name="TestNGPack1.Program2" >
          <methods>
             <include name="TC003" />
          </methods>
          </class>
          <class name="TestNGPack2.Program3" >
          <methods>
             <include name="TC005" />
          </methods>
          </class>
          <class name="TestNGPack2.Program4" >
          <methods>
             <include name="TC007" />
          </methods>
          </class>
    </classes>
 </test> 
 </suite>

In the above xml file <methods> tag defines the group of methods and <include> tag defines which method you wants to include in execution.

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

As you see the above report, Only Selected methods are executed which I have included in testng.xml file.

Configure testng.xml file to exclude specific test method from class

Same way, To exclude specific test method from class, We can use <exclude> tag inside <methods> tag as bellow.

Scenario:
Now I want to execute all the methods from all the classes except following 2 methods,

TC001() method from TestNGPack1->Program1.java
TC007() method from TestNGPack2->Program4.java

How to do?

Configure testng.xml file to exclude the above 2 methods from corresponding classes.
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" >
 	<classes>
        <class name="TestNGPack1.Program1" >
          <methods>
             <exclude name="TC001" />
          </methods>
         </class>
         <class name="TestNGPack1.Program2" />
         <class name="TestNGPack2.Program3" />
         <class name="TestNGPack2.Program4" >
          <methods>
             <exclude name="TC007" />
          </methods>
          </class>
    </classes>
  </test> 
 </suite>

Here I have used exclude<> tag inside method<> tag to not execute TC001() and TC007() methods.

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

As you see the above report TC001() and TC007() methods are not Executed.

This way we can configure our testng.xml file to include or exclude any specific test method from execution.

 

Include/Exclude Selected Test packages Using testng.xml File

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.

 

 

Using Regular Expression To Include/Exclude Test Method From Execution

In my previous post I have explained testng.xml configuration to include/exclude selected test methods or packages in execution. There is also another way of including or excluding selected test method using regular expression. Let me describe it with one simple example.

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

Each Class containing two methods as shown in below image.

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 TCXYZ001() {
		System.out.println("====TCXYZ001 Executed====");
	}@Test
	public void TCABC002() {
		System.out.println("====TCABC002 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 TCXYZ003() {
		System.out.println("====TCXYZ003 Executed====");
	}@Test
	public void TCABC004() {
		System.out.println("====TCABC004 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 TCXYZ005() {
		System.out.println("====TCXYZ005 Executed====");
	}@Test
	public void TCABC006() {
		System.out.println("====TCABC006 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 TCXYZ007() {
		System.out.println("====TCXYZ007 Executed====");
	}@Test
	public void TCABC008() {
		System.out.println("====TCABC008 Executed====");
	}
}

Scenario:
I want to execute test methods containing “XYZ” String in their name from all the packages.

 To perform above scenario, configure the testng.xml file using regular expression as below.

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

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

If you see in above given test execution report, only methods containing “XYZ” String in their name are executed as per our expectation.

This way you can use regular expressions for method’s exclusion using <exclude> tag.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *