In this post, we will discuss the include and exclude tags of TestNG in detail. Below are the points that we are going to look at in this post –
- Include and exclude packages
- Second, Include and exclude test methods
- And lastly, Include and exclude groups
We can only use the <exclude> tag with the packages, methods, and run tag ( subtag of the groups that tell us which groups to run ). Let’s look at all of the points mentioned above.
Include and exclude packages
In this, we will discuss packages and sub-packages and what we would need to be done to include or exclude some packages. Below is the structure that we would be using in our examples.

Here Practice is our top-level package, whereas Test1, Test2, and Test3 are our child packages, and each of them contains one Test class.
CodekruTest1.java
public class CodekruTest1 {
@Test
public void test1() {
System.out.println("Executing test1 in CodekruTest1 class");
Assert.assertTrue(true);
}
}
CodekruTest2.java
public class CodekruTest2 {
@Test
public void test1() {
System.out.println("Executing test1 in CodekruTest2 class");
Assert.assertTrue(true);
}
}
CodekruTest3.java
public class CodekruTest3 {
@Test
public void test1() {
System.out.println("Executing test1 in CodekruTest3 class");
Assert.assertTrue(true);
}
}
Now, if we want to run all of the test cases within the package Practice, we would make our XML file as shown below.
<suite name="codekru">
<test name="codekruTest">
<packages>
<package name="Practice.*" />
</packages>
</test>
</suite>
Output after running the above XML file
Executing test1 in CodekruTest1 class
Executing test1 in CodekruTest2 class
Executing test1 in CodekruTest3 class
Include some specific packages only
Now, what if we only want to run some specific packages, like packages Test1 and Test3? Instead of writing the * ( asterisk ) in the XML file, we will have to write the exact path of the package that we want to run.
<suite name="codekru">
<test name="codekruTest">
<packages>
<package name="Practice.Test1" />
<package name="Practice.Test3" />
</packages>
</test>
</suite>
Output after running the above XML file
Executing test1 in CodekruTest1 class
Executing test1 in CodekruTest3 class
Exclude some specific packages only
Here we will be using the <exclude> tag to exclude some specific packages from the execution. Let’s try to exclude package Test1 from the execution.
<suite name="codekru">
<test name="codekruTest">
<packages>
<package name="Practice.*" >
<exclude name = "Practice.Test1"/>
</package>
</packages>
</test>
</suite>
Output after running the above XML file
Executing test1 in CodekruTest2 class
Executing test1 in CodekruTest3 class
Include and exclude test methods
This is one of the most used use case of <include> and <exclude> tags. Sometimes, we want to run only a few cases from a class instead of running all of them, and we can achieve this by using the <include> tag. And similarly, if we want to exclude some particular test cases or test methods, we can do so by using the <exclude> tag.
Include some specific test methods only
To include some of the test methods only from a test class, we have to use the <include> tag as shown below.
Syntax
<class name = "name_of_the_class">
<methods>
<include name = "name_of_the_test_method"/>
</methods>
</class>
Let’s take a class named CodekruTest, having 4 test methods, and then we will try to run only 2 of them.
CodekruTest.java
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTest {
@Test
public void test1() {
System.out.println("Executing test1 in CodekruTest class");
Assert.assertTrue(true);
}
@Test
public void test2() {
System.out.println("Executing test2 in CodekruTest class");
Assert.assertTrue(true);
}
@Test
public void test3() {
System.out.println("Executing test3 in CodekruTest class");
Assert.assertTrue(true);
}
@Test
public void test4() {
System.out.println("Executing test4 in CodekruTest class");
Assert.assertTrue(true);
}
}
Below is the XML file
<suite name="codekru">
<test name="codekruTest">
<classes>
<class name = "Test.CodekruTest">
<methods>
<include name="test1" />
<include name="test3"/>
</methods>
</class>
</classes>
</test>
</suite>
Output after running the XML file –
Executing test1 in CodekruTest class
Executing test3 in CodekruTest class
Here, we can see that only test1 and test3 got executed.
Exclude some specific test methods only
The syntax of the <exclude> tag is similar to that of the <include> tag
<class name = "name_of_the_class">
<methods>
<exclude name = "name_of_the_test_method"/>
</methods>
</class>
Now, let’s exclude the test3() method from execution in the same CodekruTest class.
<suite name="codekru">
<test name="codekruTest">
<classes>
<class name = "Test.CodekruTest">
<methods>
<exclude name = "test3"/>
</methods>
</class>
</classes>
</test>
</suite>
Output after running the XML file
Executing test1 in CodekruTest class
Executing test2 in CodekruTest class
Executing test4 in CodekruTest class
Here we can see that the test3() method didn’t execute.
Include and exclude groups
We have explained TestNG groups in one of our previous articles, but we will explain the basics of including and excluding groups.
Let’s take a class that we will use to explain the inclusion and exclusion of groups in our examples.
CodekruTest.java
public class CodekruTest {
@Test(groups = { "group1","group2" })
public void test1() {
System.out.println("Executing test1 in CodekruTest class");
Assert.assertTrue(true);
}
@Test(groups = { "group2" })
public void test2() {
System.out.println("Executing test2 in CodekruTest class");
Assert.assertTrue(true);
}
@Test(groups = { "group1" })
public void test3() {
System.out.println("Executing test3 in CodekruTest class");
Assert.assertTrue(true);
}
}
Including specific groups
Let’s try to run the cases under group1 only
<suite name="codekru">
<test name="codekruTest">
<groups>
<run>
<include name="group1" />
</run>
</groups>
<classes>
<class name="Test.CodekruTest">
</class>
</classes>
</test>
</suite>
Output after running the above XML file
Executing test1 in CodekruTest class
Executing test3 in CodekruTest class
Exclude specific groups
Now, let’s try to exclude the group1. The XML file we created above will remain the same except for changing the tag name from <include> to <exclude>.
<suite name="codekru">
<test name="codekruTest">
<groups>
<run>
<exclude name="group1" />
</run>
</groups>
<classes>
<class name="Test.CodekruTest">
</class>
</classes>
</test>
</suite>
Output after running the above XML file
Executing test2 in CodekruTest class
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].