Saturday, September 28, 2019

Post#114.How to Include-Exclude Selected Test Methods Using testng.xml File


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.

No comments:

Post a Comment