Skip or Disable Test cases

Sometimes we can face a situation where our test cases can not be prepared and we have to stop those tests from running due to exception issue’s, or we need to run only multiple test cases.

Ways to Skip or Disable Tests cases in TestNG:

  1. Making parameter “enabled” as “false
  2. Using “exclude” parameter in testng.xml
  3. By making invocationcount equals to zero

1. Making parameter “enabled=false”

package testNGFrameWork;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class testNGRunPrioritywise {
  WebDriver driver;
  
  //Open the browser	
  @Test(priority = 1)
  public void launchBrowser() {
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		
  }
  
  //Do not close the browser
  @Test(priority = 3, enabled = false)
  public void closeBrowser() {
	  driver.close();
  }
  
  //Navigate to google.com
  @Test(priority = 2)
  public void verifyTitle() {
	  driver.get("https:www.google.com");
	  String pageTitle=driver.getTitle();
	  Assert.assertEquals(pageTitle, "Google");
  }  
}

If we want to execute test method then simply set enabled as true 
e.g: @Test(enabled = true) .

2. Using “exclude” parameter in testng.xml

  • TestNG provides an option to include or exclude for Groups, Test Methods, Classes and Packages by using include and exclude tags in testng.xml
  • Only include methods will be Execute other methods will be skip

Lets see an example –

package testNGFrameWork;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class testNGRunPrioritywise {
  WebDriver driver;
	
  @Test(priority = 1)
  public void launchBrowser() {
		System.setProperty("webdriver.chrome.driver", "F:\\Software\\programming Software\\Selenium With Java\\chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		
  }
  
  @Test(priority = 3)
  public void closeBrowser() {
	  driver.close();
  }
  
  @Test(priority = 2)
  public void verifyTitle() {
	  driver.get("https:www.google.com");
	  String pageTitle=driver.getTitle();
	  Assert.assertEquals(pageTitle, "Google");
  }  
}

Now if we want to skip close browser method then we need to make some changes on XML file. Lets see the below XML file-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
	<test thread-count="5" name="Test">
		<classes>
			<class name="testNGFrameWork.testNGRunPrioritywise">
				<methods>
					<exclude name="closeBrowser" />
				</methods>
			</class>
		</classes>
	</test> <!-- Test -->
</suite> <!-- Suite -->

Here we not mentioning include tag, if we don’t mention then TestNG execute it by default.

Similarly we can skip groups, Classes and Packages.

3. By making invocationcount equals to zero

Invocation count is used when you want to run the same tests multiple times. If invocationCount = 5, then the test method will be executed 5 times before executing next test method.

Lets see an example-

package testNGFrameWork;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class invocationCount {

	@Test(invocationCount = 0)
	public void getTitle() {
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.get("http://www.google.com");
		driver.manage().window().maximize();
		System.out.println("Website Title: " + driver.getTitle());
		driver.quit();
	}

	@Test
	public void secondTest() {
		System.out.println("===This will be executed at the end===");
	}
}

Next
TestNG
Home Page

Leave a comment

Design a site like this with WordPress.com
Get started