Include and exclude in TestNG

Earlier, we have seen how to execute all the test cases within the package or run a specific class using the testng.xml file ( Article link ). But sometimes, we might be selective while executing test cases.

  • like executing one test case from class1 and another from class2,
  • or running only 2 groups out of 10 different groups.

So, this post will focus on these scenarios where we need to include or exclude tests based on our requirements

Let’s look at them one by one.

Note: We have also attached a video at the end of this article. In case you are more comfortable with the video version, then please feel free to have a look

Include and exclude packages

In a typical Java project, you might write test cases in various packages to organize your tests or for better readability. And sometimes, we only need to execute tests of a single package, or maybe a couple of them. Then, how do we facilitate that?

TestNG enables us to include or exclude packages depending on the requirement.

We have created a simple project structure with 3 packages, each containing one test class. We have written one test case in each class file.

package structure

Practice is our top-level package, whereas Test1, Test2, and Test3 are our child packages.

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);
	}

}

If we want to run all of the test cases within the Practice package, then we can do so by using the below XML file.

<suite name="codekru">
	<test name="codekruTest">
		<packages>
			<package name="Practice.*" />
		</packages>
	</test>
</suite>

Here, we have used an asterisk (*) to tell TestNG to execute all the sub-packages inside the Practice package.

Output after running the above XML file

Executing test1 in CodekruTest1 class
Executing test1 in CodekruTest2 class
Executing test1 in CodekruTest3 class

But what if we want to run specific packages, like Test1 and Test3? So, Instead of writing the * ( asterisk ) in the XML file, we will have to write the exact path of the package 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

Here, we will use the <exclude> tag to exclude the packages from the test execution. Let’s try to exclude Test1 package 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

Sometimes, we may need to run only specific test cases from a class instead of running all of them, or we might want to run a few test cases from one class and a few from another.

We can achieve this by using <include> and <exclude> tags in TestNG.

We can use the <include> tag to include only specific methods from a class. Below syntax shows how to include a specific method

Syntax

<class name = "name_of_the_class">
  <methods>
    <include name = "name_of_the_test_method"/>
  </methods>
</class>

In case we want to include more than one method, then we would have to write as many method names with the <include> tag.

Let’s take a class named CodekruTest, which has 4 test methods. We will then 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 were executed.

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

We can that all tests excluding test3 was executed.

Include and exclude groups

Please read this article to learn about TestNG Groups.

We have created one class where we assign groups to the test cases.

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);
    }
 
}

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>

Above syntax will ensure that all the cases belonging to “group1” in the “CodekruTest” class will be executed.

Output after running the above XML file

Executing test1 in CodekruTest class
Executing test3 in CodekruTest class

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 liked the article. If you have any doubts or concerns, please feel free to write to us in the comments or email us at admin@codekru.com.

Related Articles –
Video Tutorial
Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *