- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to exclude a test class from a testing suite using testng.xml?
testng.xml has a format as <classes> where we define all the test classes that should be executed. There are no specific ways to exclude a class in <classes>, but there are workarounds that are quite useful in case you don't want to run a specific class in a testing suite.
Following are some of the handy ways to exclude a test class run from a test suite.
As usual, just mention the class names those are required to execute and remove the class names that are not supposed to execute.
Mention all the class names inside <classes> including the ones that should not be run. And, inside the class that should be excluded, use <methods> and <exclude name =.* />. It will exclude all the tests from this class and run all the other classes.
If classes are present in nested or different packages, then use the package name until the common name in <packages><package name=common_package> and <exclude name= common_package.exclude_package_name />. It will exclude the class name mentioned inside the package. However, if there are multiple files in the same package, then the above approach is useful.
In this article, we are going to discuss how to implement the last two approaches.
Scenario 1
Excluding all the methods from a class to exclude a class execution from a suite. Here, we will have 2 classes with multiple test methods, and we will see how testng.xml is configured to run only one class "NewTestngClass" and exclude the other class.
Approach/Algorithm to solve this problem
Step 1 − Create two TestNG classes: NewTestngClass and OrderofTestExecutionInTestNG.
Step 2 − Write two different @Test methods in both the classes.
Step 3 − Create the testNG.xml as given below.
Step 4 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
The following code shows how to run only 1 test method from a large suite −
src/ NewTestngClass.java
import org.testng.annotations.Test; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } }
src/OrderofTestExecutionInTestNG.java
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "NewTestngClass" /> <class name = "OrderofTestExecutionInTestNG" > <methods> <exclude name=".*" /> </methods> </class> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
Scenario 2
If classes are present in nested or different packages, then use the tag <packages> to exclude a test class from a test suite.
The package name should be in format of common_packages.excluding_class_package. Here, we will have 2 classes with multiple test methods, and we will see how testng.xml is configured to run only one class "NewTestngClass" and exclude the other class.
Approach/Algorithm to solve this problem −
Step 1 − Crete a common package under src as com.test
Step 2 − Create another package inside com.test as exclude.class
Step 3 − Create two TestNG classes: NewTestngClass inside com.test package and OrderofTestExecutionInTestNG inside exclude.class.
Step 4 − Write two different @Test methods in both of the classes: NewTestngClass and OrderofTestExecutionInTestNG.
Step 5 − Now create the testNG.xml as given below.
Step 6 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
The following code shows how to run only 1 test method from a large suite −
src/ com.test.NewTestngClass.java
package com.test; import org.testng.annotations.Test; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } }
src/com.test.exclude.class.OrderofTestExecutionInTestNG.java
package com.test.exclude.class; import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
testng.xml
This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <packages> <package name="com.test.*"> <exclude name="com.test.exclude.class" /> </package> </packages> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How to execute a single test from a large testing suite using TestNG.xml?
- How to exclude a test from execution in Pytest?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to include and exclude test methods from a set of test cases in\nCucumber?
- How to retrieve all test methods name in TestNG suite?
- How to use regular expression in class path in TestNG.xml?
- How to exclude a field from JSON using @Expose annotation in Java?
- How to force end an entire test suite from the BeforeSuite annotation if a condition is met in TestNG?
- How to retrieve the test suite name at runtime in TestNG?
- How to exclude a specific row from a table in MySQL?
- How to test if an element has class using Protractor?
- FabricJS – How to exclude Line object from being saved while using JSON.stringify()?
- How to convert a bean to JSON object using Exclude Filter in Java?
- How to exclude Text object from being saved while using JSON.stringify() in FabricJS?
- How to Test a Transistor Using a Digital Multimeter
