TestNg With Selenium WebDriver : Using Regular Expression To Include/Exclude Test Method From Execution

It is very important for us to know the each and every way of testng.xml configuration to include/exclude selected test methods or packages in execution. You can view my posts to know how to include/exclude SELECTED PACKAGE or SELECTED TEST METHODS from selenium webdriver test suite execution. There is also one another way of including or excluding selected test method using regular expression. Let me describe it with one simple example.

For that you need to configure "TestNGOne" project using 3 packages as shown in THIS POST. Now let we configure testng.xml file.

Including/Excluding test methods using regular expression

Supposing, I wants to Include testmethodtwo() from TestNGOnePack -> ClassOne.java file and Exclude testmethodtwo() from TestNGTwoPack -> ClassTwo.java file using regular expression. For that my testng.xml file configuration will be looks like bellow.

<suite name="Suite One">
  <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne">
    <methods>
     <include name=".*two.*"/>
    </methods>
   </class>
   <class name="TestNGTwoPack.ClassTwo">
    <methods>
     <exclude name=".*two.*"/>
    </methods>
   </class>
  </classes>
 </test>
</suite>

If you see in above testng.xml file, ".*two.*" is regular expression used for test methods. Means test methods containing "two" word will be considered for inclusion/exclusion in test suite execution.. Now when I will run above testng.xml file, Selenium webdriver test execution report will looks like bellow.


If you see in above given test execution report, only two methods has been executed as per our expectation. This way you can use regular expressions for method's inclusion or exclusion.

No comments:

Post a Comment