Saturday, September 28, 2019

Post#116.Using Regular Expression To Include-Exclude Test Method 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.


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 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.


No comments:

Post a Comment