First of all, we need to understand there are two ways to skip any testcase from execution.
One is using “enabled=false” property,
The second one is using “Conditional Skip”.

The above interview question is about “Conditional Skip” using skipException from TestNG.

So, why we need to skip any testcase from execution? Let’s see some practical scenarios before writing the sample code.

Practical Scenario #1: If you’re doing some data validations from DB, if the data is not available in the Database, then there is no need to proceed for further execution of testcase.

Practical Scenario #2: if the user doesn’t have permission to access the functionality, then you can skip the execution of testcase.

Practical Scenario #3: If the target file is not accessible [may be some permission issues] , you can skip the execution of testcase.

For conditional skip, we call the constructor from SkipException Class in TestNG. Here is the snipper, which we’re going to trigger.

skip-exception-constructor

Example Program to Understand Conditional Skip using skipException:

Note: Please create an Empty text file in any location. We can use it in our program.

Output:

Explanation: In the above program, we checked the text in the file, since it is empty I don’t want to proceed further so, I used skipException from TestNG.

This Interview Question is contributed by Aman. If you like SDETForum and would like to contribute, you can also write an article & mail it to sdetforum@gmail.com.