Home » Exclude Include Test Cases in TestNG

Exclude Include Test Cases in TestNG

by Online Tutorials Library

Exclude/Include test cases

TestNG provides the feature of enabling and disabling the test cases. We can disable a set of test cases from getting executed. For example, consider a scenario where a serious bug occurs in a feature due to certain tests, so we need to disable the test cases from being executed.

In TestNG, test cases can be disabled in two ways:

  • You can disable the test case in a @Test annotation.
  • You can disable the test case in the XML file.

TestNG @Test enable parameter

You can disable or exclude the test cases by using the enable attribute to the @Test annotation and assign False value to the enable attribute.

Let’s understand through an example:

In the above code, we created two test cases, i.e., test1() and test2(), and we disable the first test case by assigning the False value to the enable attribute. On disabling the first test case, the only second test case will run.

Output

Exclude/Include test cases

In the above output, we observe that only the second test case run without any failure.

Disable the enable attribute in the XML file.

Step1: Till now, we have created the test cases of the Personal loan department.

module1.java

module2.java

module1.java contains the test cases of Personal loan while module2.java contains the test cases of a car loan.

Step 2: If we want to disable the MobileLoginCarLoan test case in car loan module, then we need to add the <method> tag in xml file which has access to all the methods of a class.

Step 3: Run the testng.xml file. Right click on the testng.xml file, and move the cursor down, you will see the Run As and then click on the 1 TestNG Suite.

Exclude/Include test cases

Output

Exclude/Include test cases

In the above output, we observe that all the test cases run successfully except the test case “MobileLoginCarLoan” which we have excluded.

Note: Suppose we have multiple test cases and you want to include only one or two test cases, in such situation, we use <include> tag. If we use the <exclude> tag, then it becomes very tedious to exclude all the test cases.

Let’s understand through an example of <include> tag.

test.java file

testng.xml file

Output

Exclude/Include test cases

In the above output, we observe that only one test case is running, i.e., test3 as we have included only test3 in <include> tag in an XML file.


You may also like