Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

TestNG Tutorials 41: Skipping a Test Conditionally in TestNG | Make Selenium Easy

Posted on 11/17/2018 By admin

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

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

Uncategorized

Post navigation

Previous Post: Understand Verification & Validation In Software Testing | Make Selenium Easy
Next Post: Handling Website Popups In Selenium webdriver | Make Selenium Easy

Related Posts

image – Make Selenium Easy Uncategorized
July 27, 2018 – Make Selenium Easy Uncategorized
June 2018 – Make Selenium Easy Uncategorized
Conditional4 – Make Selenium Easy Uncategorized
Why Should We Upcast Browser Driver Class Object To WebDriver? Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark