- 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
What is the Best Practice to Skip a Test in TestNG?
TestNG supports multiple ways to skip or ignore a @Test execution. Based on requirement, user can skip a complete test without executing it at all or skip a test based on specific condition. If condition meets at the time of execution, it skips remaining code in the test.
There are following 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 − User can have a condition check, if condition is met, it will throw SkipException and skip rest of the code.
But when we are talking about best practice to skip a test case. It depends on multiple factors such as whether the functionality is not available or there is a bug for the test or there is a dependency on this test for further execution.
In this article, we will illustrate what is the best practice to skip a test in a class when there is a dependency on the skipping test.
Approach/Algorithm to Solve this Problem
Step 1: Create a TestNG class − NewTestngClass.
Step 2: Write 3 different @Test method in the classe − NewTestngClass as shown in programming code section.
1st @Test Method:
It will execute as like simple method.
2nd @Test Method:
It throws SkipException. TestNG will print 1st line of code and skip after it as soon as reach to SkipExecution code. There is a static method that actually skip the test and the same method is called inside the @Test so that it can be easily ignored and no further changes re required.
3rd @Test Method:
It is dependent on 2nd @test Method.
Step 3: Now create the testNG.xml as given below to run the TestNG classes.
Step 4: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
Example
The following code for 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(){ System.out.println("Testcase 2 - skip exception example"); skipTest("BUG-1000-Functionality is Broken"); System.out.println("Testcase 2 - Do Not execute"); } @Test(dependsOnMethods = { "testcase2" }) public void testcase3(){ System.out.println("Test Case3 - Conditional Skip"); } public static void skipTest(String reason) { throw new SkipException("Test voluntarily skipped. Reason: " + reason); } }
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 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 2 - skip exception example Test ignored. Testcase 1 - executed Test ignored. =============================================== suite Total tests run: 3, Passes: 1, Failures: 0, Skips: 2 ===============================================