TestNG Tutorials 41: Skipping a Test Conditionally in TestNG

Hello Folks,

In this post we will learn below topics:
1. How to skip a test conditionally?
2. How conditionally skipped test is reported in TestNG report?
3. How to report conditionally skipped test case as failed in TestNG report instead of skipped?
4. What is SkipException class?
5. What is use of isSkip() method?
6. What is reduceStackTrace() method?

Sometimes, we need to run our scripts based on some conditions and this is decided at run time. For example: You have a flag value which is optional and provided at run time. If it is not passed, you need to execute Test1 and Test2 both. If flag is passed as true you need to run only Test1 and ignore Test2. If flag is passed as false you need to run only Test2 and ignore Test1. Are you able to get here that what method should be executed, is completely dependent on flag value i.e. at run time? If you know in advance, then you can simply set attribute “enabled” as false but which is not the case here. You need to skip this at run time conditionally.

This we can achieve in TestNG using SkipException class. SkipException class is a Runtime Exception class i.e. can be thrown at run time. If we want to skip a test just throw an object of this exception class. We will see an example below:

package GroupsExample;

import org.testng.SkipException;
import org.testng.annotations.Test;

public class SkipExample {

/*
* SUppose this flag will be passed from some data source. For example, I assign
* some value here. I am taking it as String just to ignore default value of
* global variable
*/
String flag = "false";

@Test
public void Test1() {
if (flag.equals("true")) {
System.out.println("Test 1 will be executed and Test 2 will be skipped.");
} else {
throw new SkipException("Skipping Test 1 as flag is false.");
}

}

@Test
public void Test2() {
if (flag.equals("false")) {
System.out.println("Test 2 will be executed and Test 1 will be skipped.");
} else {
throw new SkipException("Skipping Test 2 as flag is true.");
}

}

}

Output:

You can see in above output that conditionally skipped test is counted as skipped in TestNG report. We are good now and know how can we skip test conditionally.

Another scenario:

You are supposed to run both Test methods : Test1 and Test2 and expecting flag as empty. If it is provided as true or false, you run only one and you want to report another method as FAILED instead of SKIPPED as you wanted to execute both. So problem here is that how you will let TestNG to log skipped test as failed? Answer is again SkipException class which provides a method isSkiped().

isSkipped() method by default marks a skipped test as skipped. If we need to change behaviour, we need to override isSkipped() method. We will see an example below:

package GroupsExample;

import org.testng.SkipException;
import org.testng.annotations.Test;

// Extending SkipException class
public class SkipExampleReportFailed extends SkipException{

// Constructor call
public SkipExampleReportFailed(String skipMessage) {
super(skipMessage);
// TODO Auto-generated constructor stub
}

// Overriding default behavior to mark a test as failed instead of skipped
public boolean isSkip() {
return false;
}
/*
* SUppose this flag will be passed from some data source. For example, I assign
* some value here. I am taking it as String just to ignore default value of
* global variable
*/
String flag = "false";

@Test
public void Test1() {
if (flag.equals("true")) {
System.out.println("Test 1 will be executed and Test 2 will be skipped.");
} else {
throw new SkipExampleReportFailed("Skipping Test 1 as flag is false.");
}

}

@Test
public void Test2() {
if (flag.equals("false")) {
System.out.println("Test 2 will be executed and Test 1 will be skipped.");
} else {
throw new SkipExampleReportFailed("Skipping Test 2 as flag is true.");
}

}

}

Output:

Learn little more:

Do you see a long stack trace which is not required actually. We don’t require those stack trace as we are skipping conditionally. You can remove unwanted stack trace as well using reduceStackTrace() method. We will see an example below:


import org.testng.SkipException;
import org.testng.annotations.Test;

public class SkipExampleWithReducedStackTRace extends SkipException {

// Constructor call
public SkipExampleWithReducedStackTRace(String skipMessage) {
super(skipMessage);
// TODO Auto-generated constructor stub
}

/*
* SUppose this flag will be passed from some data source. For example, I assign
* some value here. I am taking it as String just to ignore default value of
* global variable
*/
String flag = "false";

@Test
public void Test1() {
if (flag.equals("true")) {
System.out.println("Test 1 will be executed and Test 2 will be skipped.");
} else {

SkipExampleWithReducedStackTRace s= new SkipExampleWithReducedStackTRace("Skipping Test 1 as flag is false.");
s.reduceStackTrace();
throw s;
}

}

@Test
public void Test2() {
if (flag.equals("false")) {
System.out.println("Test 2 will be executed and Test 1 will be skipped.");
} else {
SkipExampleWithReducedStackTRace s= new SkipExampleWithReducedStackTRace("Skipping Test 2 as flag is true.");
s.reduceStackTrace();
throw s;
}

}

}

Output:

 

So now we know answers of below questions:

1. How to skip a test conditionally? ==> Using skipException class.
2. How conditionally skipped test is reported in TestNG report? ==> As a skipped test case.
3. How to report conditionally skipped test case as failed in TestNG report instead of skipped? ==> By overriding isSkipped() method.
4. What is SkipException class? ==> Helps us to skip a test conditionally.
5. What is use of isSkip() method? ==> Flag if the current exception marks a skipped method or a failure
6. What is reduceStackTrace() method? ==> To reduce stack trace for a skipped test

More about TestNG in upcoming posts. Stay tuned.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning