Skip to content Skip to navigation
Check Our New Demo Website!

Include and Exclude Test Methods in TestNG

TestNg provides an option to include or exclude Groups, Test Methods, Classes and Packages using include and exclude tags by defining in testng.xml.

First we will create an examples to use include and exclude tags for Test Methods in a class.

We will create a Class with three Test Methods. In that we will include two test methods and try to exclude one test method.

Below is the example class file with three test methods

package com.easy.entry;
import org.testng.annotations.Test;

public class AddTestCase {
	@Test
	public void addLocationTestCase() {
		System.out.println("Im in add location test case");
	}
	
	@Test
	public void addDepartmentTestCase() {
		System.out.println("Im in add department test case");
	}
	
	@Test
	public void addEmployeeTestCase() {
		System.out.println("Im in add employee test case");
	}
}

In the above class example, we have created three test methods, 'addLocationTestCase', 'addDepartmentTestCase', and
'addEmployeeTestCase'.

In the below testng.xml file we will exclude 'addEmployeeTestCase' and try to execute the program. We need to first add the class name and in that class , we need to define the methods which needs to be included and excluded

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sample Test Suite" verbose="1" >
  <test name="Method Test Cases" >
    <classes>
       <class name="com.easy.entry.AddTestCase">
        <methods>
        <include name="addLocationTestCase" />
        <include name="addDepartmentTestCase" />
        <exclude name="addEmployeeTestCase" />
      </methods>
       </class>
    </classes>
  </test>
</suite>

After running the above testng.xml file, we will get the output as below. It will just run the test methods which are included in the class. And will not execute the test methods which are in excluded.

include exclude tests

Here as in the above xml file, we can also just mention test methods which you want to exclude. If say there are 20 test methods in a class, and if you just want to exclude one or two test methods, you can just have exclude tag with test methods without again adding all the other tests methods using include tag.

Look at the below xml file to exclude only the particular test methods which are needed. In the below example, we are trying to exclude only one test method. And all the test methods in a class will get executed by default.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sample Test Suite" verbose="1" >
  <test name="Method Test Cases" >
    <classes>
       <class name="com.easy.entry.AddTestCase">
        <methods>
        <exclude name="addEmployeeTestCase" />
      </methods>
       </class>
    </classes>

Below is the same output which is generated.

include exclude tests

Here again same as above, if you just want to execute only one or two test methods, we can simply mention only those test methods in the testng.xml file. Only those test methods will be executed and all the rest test methods will not be executed.

Look at the below xml file to include only particular test methods

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Sample Test Suite" verbose="1" >
  <test name="Method Test Cases" >
    <classes>
       <class name="com.easy.entry.AddTestCase">
        <methods>
        <include name="addDepartmentTestCase" />
      </methods>
       </class>
    </classes>
  </test>
</suite>

Below is the same output which is generated. Even though there are three test methods in the class, we are trying to execute only one particular test method by specifying the test method in include tag.

testng include tests

In the same way we can also use include and exclude tags for Groups, Classes and Packages

Test Frameworks: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.