Using Regular Expression To Include/Exclude Test Method From Execution

Read Full Article 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.

site de rencontre pour celibataire au cameroun Step 1: Create package = “ realtors dating site TestNGPack1” and under this package Create classes as  http://armor-deck.net/edikpedik/6107 Program1.java and  Related Site Program2.java
http://mariechristinedesign.com/?misleno=rencontre-sur-nantes&e7a=6a Step 2: Create package = “ http://makse.com/?kremel=celebrity-hookup-2014&343=0e TestNGPack2” and under this package Create classes as  http://boersenalltag.de/blog/blog-from/2014-10-01/blog-to/2014-10-31/index.html Program3.java and  browse around this website Program4.java

Each Class containing two methods as shown in below image.

Once done with above steps your Project structure will look like this.

 

http://thenovello.com/alfondie/elkos/122 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.