- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 a TestNG test based on a condition?
TestNG supports multiple ways to skip or ignore a @Test execution. Based on the requirement, a user can skip a complete test without executing it at all or skip a test based on a specific condition. If the condition meets at the time of execution, it skips the remaining code in the test.
Following are the ways to skip the @Test execution −
Use the parameter enabled=false at @Test. By default, this parameter is set as True.
Use throw new SkipException(String message) to skip a test.
Conditional Skip – The user can have a condition check. If the condition is met, it will throw SkipException and skip the rest of the code.
In this article, we will illustrate how to do conditional skipping.
Approach/Algorithm to solve this problem −
Step 1 − Create a TestNG class called NewTestngClass.
Step 2 − Write two different @Test methods in the class, as shown in the program code.
1st @Test Method − It is enabled=true. It will execute and TestNG will print the output.
2nd @Test Method − It is conditional skip. The code checks whether the DataAvailable parameter is True or False. If it is False, it throws SkipException and skips the test. But, if DataAvailable is True, it won't throw the SkipException and continue the execution.
Step 3 − Now create the testNG.xml 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; public class NewTestngClass { @Test() public void testcase1(){ System.out.println("Testcase 1 - executed"); } @Test public void testcase2(){ boolean DataAvailable=false; System.out.println("Test Case2 - 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
Testcase 1 - executed Test Case2 - Conditional Skip Test ignored. =============================================== Default Suite Total tests run: 2, Passes: 1, Failures: 0, Skips: 1 ===============================================
- Related Articles
- How to disable an entire unit test in TestNG?
- How does running a specific test method depend on another test method in TestNG?
- How will you run a prerequisite method and post condition method for every test in TestNG?
- Find MongoDB records based on a condition?
- Change string based on a condition - JavaScript
- How to skip a particular test method in TestNG?
- How to force end an entire test suite from the BeforeSuite annotation if a condition is met in TestNG?
- SUM a column based on a condition in MySQL
- How does the execution of a particular test method depend on other test methods in TestNG?
- MySQL query to update different fields based on a condition?
- ORDER BY records in MySQL based on a condition
- How to skip TestNG test at runtime?
- How to run test groups in TestNG?
- How to find the frequency for all columns based on a condition in R?
- How to overlook a particular test method from execution in TestNG?
