- 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 disable an entire unit test in TestNG?
TestNG supports multiple ways to ignore all @Test execution. If required, a user can ignore a complete test without executing it at all. TestNG supports ignoring all @Test at the following levels −
In a class
In a particular package
In a package and all of its child package
The user has to use @Ignore annotation at the required level to disable the tests. The @Ignore annotation takes higher priority than individual @Test annotation.
To disable all the @Test in a class, just type @Ignore before the class name. It will disable all the @Test present inside the class.
In this article, we will illustrate how to disable an entire test in a class.
Approach/Algorithm to solve this problem −
Step 1 − Create a TestNG class called NewTestngClass.
Step 2 − Write two different @Test methods in the class and place the @Ignore annotation just above the class name as shown in programming code section.
Step 3 − Now create the testNG.xml as given below to run the TestNG classes.
Step 4 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
Use the following code for the common TestNG class "NewTestngClass" −
src/ NewTestngClass.java
import org.testng.SkipException; import org.testng.annotations.Test; @Ignore public class NewTestngClass { @Test(enabled=false) public void testcase1(){ System.out.println("Testcase 1 - Not executed"); } @Test public void testcase2(){ System.out.println("Testcase 2 - skip exception example"); throw new SkipException("Skipping this exception"); } @Test public void testcase3(){ boolean DataAvailable=false; System.out.println("Test Case3 - Conditional Skip"); if(!DataAvailable) throw new SkipException("Skipping this exception"); System.out.println("Executed Successfully"); } }
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"/> </classes> </test> </suite>
Output
=============================================== Suite1 Total tests run: 0, Passes: 0, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How to disable a TestNG test based on a condition?
- How to force end an entire test suite from the BeforeSuite annotation if a condition is met in TestNG?
- How to run test groups in TestNG?
- How to run multiple test classes in TestNG?
- How to skip TestNG test at runtime?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to skip a particular test method in TestNG?
- How to set priority to the test cases in TestNG?
- How to combine multiple groups to single Test in TestNG?
- How to retrieve all test methods name in TestNG suite?
- How to overlook a particular test method from execution in TestNG?
- How to retrieve the test suite name at runtime in TestNG?
- How to pass a variable from BeforeTest to Test annotation in TestNG?
- How to retrieve test method description in TestNG before and after execution?
- How to retrieve test method name in TestNG before and after execution?
