@Test Attributes

HOW TO SKIP EXCEPTION TEST IN TESTNG

Using TestNG, we have multiple ways to Skip a test based on our requirement. We can Skip complete test without executing it or we can Skip a test when a specific condition is not satisfied.

Case 1: In TestNG, @Test(enabled=false) annotation is used to skip a test case if it is not ready to test. We don’t need to import any additional statements.

As in JUnit, TestNG will not show you the other test method as Skipped or Ignored. It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”.

Case 2: And We can Skip a test by using TestNG Skip Exception if we want to Skip a particular Test.
Syntax: throw new SkipException(“message”);

Case 3: we can also perform a CONDITIONALSkip, i.e. we can have a condition to check and then SKIP test if condition is not satisfied. For Example, if there is no data available to perform a test, we can skip the test instead of making that test as failed.

Let us see all the above three cases by taking three simple tests.

Please find the below sample program.

package TestNGPack2;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class SkipExample {

	public static WebDriver driver = new FirefoxDriver();
	String DataAvailable = "null";

	@BeforeTest
	public void setup() throws Exception {
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
		driver.get("http://newtours.demoaut.com/");
	}

	@Test(enabled = false)
	public void TC001() {
		System.out.println("TC001  Executed");
	}@Test
	public void TC002() {
		System.out.println("I am in TC002 - Intensional Skip");
		String title = driver.getTitle();
		if (title.equals("Welcome: Mercury tours")) {
			//To Skip Test
			throw new SkipException("TC002 Is Skipped");
		}
		System.out.println("TC002 Executed Successfully");
	}

	@Test
	public void TC003() {
		System.out.println("Im in Conditional Skip");
		if (DataAvailable.equals("null")) {
			throw new SkipException("TC003 is Skipped - Data Not Available");
		}

		System.out.println("TC003 Executed Successfully");
	}@AfterTest
	public void tearDown() throws Exception {
		driver.quit();
	}

}
Once executed the above program the output will look as below.

As you see the above output,

TC001() method is not executed at all. Because It will not consider that case method at all when the annotation is mentioned as “@Test(enabled=false)”

TC002() method is Executed and passed. Because The Page Title is matched, so it did not go inside if condition. It will skip if the Title will not match with Expected.

TC003() is Skipped. Test TC003 will be skipped only when data is not available. Here I took a simple variable DataAvailablefor ease of understanding. The ‘if condition satisfied, hence Skipped the test. You can put condition according your requirement.

 

Prioritizing Tests in TestNG:

Prioritizing tests in testng will discuss about the order of execution tests in test suite. We will write test methods our own way using @Test annotation. After writing the test cases we will execute the test cases either normally or from the testng.xml file.

After execution of the tests if we observe the order of execution then we can find that tests executed by taking the alphabetical order. And all the tests will have the equal priority as we did not set any priority to the tests.

In order to set the priority to the tests then we can use one of the @Test attributes called “Priority”. By default all the tests will have the same priority called Zero(i.e. If you not set any priority then it will take the priority as Zero).

Let me explain this with below example.

First of all create one Class as PriorityExample.Java as below.

PriorityExample.Java

package PrioritySet;

import org.testng.annotations.Test;

public class PriorityExample {@Test
	public void Register() {
		System.out.println("===Register TestCase Executed===");
	}@Test
	public void Login() {
		System.out.println("===Login TestCase Executed===");
	}@Test
	public void LogOut() {
		System.out.println("===LogOut TestCase Executed===");
	}
}

If you will run above test in eclipse then console output will looks like bellow.

===LogOut TestCase Executed===
===Login TestCase Executed===
===Register TestCase Executed===

If we observe the above output of all the 3 methods they executed in the alphabetical order.

As per console output, LogOut() @Test method Is executed first, then Login() @Test method and at last Register() @Test method. So this is strange and not as per my requirement. It should execute Register() @Test 1st, Login()@Test 2nd and LogOut() @Test 3rd.

Above issue can be resolve by setting Priority of test.

You can set priority with @Test annotation to correct test execution sequence as bellow.

PriorityExample.Java

package PrioritySet;

import org.testng.annotations.Test;

public class PriorityExample {@Test(priority = 1)
	public void Register() {
		System.out.println("===Register TestCase Executed===");
	}@Test(priority = 2)
	public void Login() {
		System.out.println("===Login TestCase Executed===");
	}@Test(priority = 3)
	public void LogOut() {
		System.out.println("===LogOut TestCase Executed===");
	}
}

If you will run above test in eclipse then console output will looks like bellow.

===Register TestCase Executed===
===Login TestCase Executed===
===LogOut TestCase Executed===

If we observe the above output of all the 3 methods they executed as per the priorities we set.
The test without priority executed first as the default priority is equal to ZERO and it takes the high priority.
This way you can set test execution priority for each and every @Test method of your class.

 

Preserve Order in TestNG:

Preserve Order in TestNG will discuss about the order of test case execution in testNG. Usually the order of execution in the testNG will be alphabetical order. Means, if you not set any priority then it will execute the test cases in alphabetical order.

In my previous post (i.e.Prioritizing Tests in testng) we have seen how to use the priority attribute of @Test annotation to execute the test cases in our own order.

We will write the test cases how we want to execute but by default testNG will not execute in order how we have written. For this we need to use priority attribute for each and every test to execute the test cases in predefined order. But writing priority for each test case is tedious as we may have so many test cases in our test suite.

To overcome this problem, there is one more option called “preserve-order”in the testNG. We can use this attribute at suite level or test level.  If you set this value to “true” then it will execute the tests which are mentioned in the order in testng,xml file. Means, it will now not execute the tests in alphabetical order.

Let me explain this with below example.

First of all configure your Project Structure as below.

Step 1: Create package = “PreserveOrder” and under this package Create classes as

Register.javaLogin.java, and LogOut.java

Once done with above steps your Project structure will look like this.

Register.java

package PreserveOrder;

import org.testng.annotations.Test;

public class Register {@Test
	public void TestCase() {
		System.out.println("===Register TestCase Executed===");
	}
}

Login.java

package PreserveOrder;

import org.testng.annotations.Test;

public class Login {@Test
	public void TestCase() {
		System.out.println("===Login TestCase Executed===");
	}
}

LogOut.java

package PreserveOrder;

import org.testng.annotations.Test;

public class LogOut {
  @Test
  public void TestCase() {
    System.out.println("===LogOut TestCase Executed===");
  }
}

Now we will define the xml file with preserve-order attribute for tests and set the value as ‘false‘.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Preserve order test runs">
  <test name="Regression Test" preserve-order="false">
    <classes>
      <class name="PreserveOrder.Register"/>
      <class name="PreserveOrder.Login"/>
      <class name="PreserveOrder.LogOut"/>
      </classes>
  </test>
</suite>

Output:

[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===LogOut TestCase Executed===
===Login TestCase Executed===
===Register TestCase Executed===

===============================================
Preserve order test runs
Total tests run: 3, Failures: 0, Skips: 0
===============================================

As we have set the preserve-order attribute to false, test will not be executed in order. They will get executed in an unpredictable order.

You can see the above output test execution sequence; it is not same as given In testng.xml file.

Now let us use preserve-order=”true” attribute in testng.xml file as bellow and observe execution result.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Preserve order test runs">
  <test name="Regression Test" preserve-order="true">
    <classes>
   	 <class name="PreserveOrder.Register"/>
      <class name="PreserveOrder.Login"/>
      <class name="PreserveOrder.LogOut"/>
    </classes>
  </test>
</suite>

OutPut:

[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===Register TestCase Executed===
===Login TestCase Executed===
===LogOut TestCase Executed===

===============================================
Preserve order test runs
Total tests run: 3, Failures: 0, Skips: 0
===============================================

As you see the above output, Test is executed in order as configured in testng.xml file.

So this is the example of using preserve-order attribute in testng.xml file. To execute test cases in correct order, you need to set preserve-order=true and to execute them in unpredictable order, set preserve-order=false

 

Timeout Test in TestNG:

While running tests there can be cases where certain tests get stuck or may take much more time than expected. In such a case you may need to mark the said test case as fail and then continue. In this tutorial, we will learn to configure TestNG tests to timeout after some configured time duration.

TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:

  • At suite level:This will be applicable for all the tests in the said TestNG test suite
  • At each test method level:This will be applicable for the said test method and will override the time period if configured at the suite level

To specify timeout duration, use “timeOut” attribute of @Test annotation.

@Test (timeOut = 500)

Let’s create a sample test and learn how timeout works in TestNG.

Example 1: Timeout Test at Suite Level

First of all create one Class as TimeoutTest.Java as below.

package Timeout;

import org.testng.annotations.Test;

public class TimeoutTest {@Test
	public void TestOne() throws InterruptedException {
		Thread.sleep(1000);
		System.out.println("===TimeOut test method One Executed===");
	}

	@Test
	public void TestTwo() throws InterruptedException {
		Thread.sleep(400);
		System.out.println("===TimeOut test method Two Executed===");
	}
}

In above class, we have two test methods i.e. TestOne() and TestTwo().

TestOne() will take 1000ms to execute completely whereas TestTwo() will take 400ms to execute completely. We have enforced the execution time using Thread.sleep() method.

Now configure the testng.xml file as below. This code defines timeout period to 500ms.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TimeOut test Suite" time-out="500" verbose="1" >
  <test name="Timeout Test">
    <classes>
   	 <class name="Timeout.TimeoutTest"/>
     </classes>
  </test>
</suite>

Now run above tests using testng.xml. Output of above test run is given below:

[TestNG] Running:
  D:\WorkSpace\DemoTestNG\testng.xml

===TimeOut test method Two Executed===

===============================================
TimeOut test Suite
Total tests run: 2, Failures: 1, Skips: 0
===============================================

As you can see from the test results, only TestTwo() for executed because it’s execution time was less than timeout time defined in testng.xml file. TestOne() execution got cancelled because it took more time to complete than timeout duration configured.

Example 2: Timeout Test at Method Level

In the previous example we are defining the time at Suite Level in testng.xml file. But in method level Time Out Test you can define the time using “timeOut” attribute of @Test annotation as below.

TimeoutTest.java

package Timeout;

import org.testng.annotations.Test;

public class TimeoutTest {@Test(timeOut = 500)
	public void TestOne() throws InterruptedException {
		Thread.sleep(1000);
		System.out.println("===TimeOut test method One Executed===");
	}

	@Test(timeOut = 500)
	public void TestTwo() throws InterruptedException {
		Thread.sleep(400);
		System.out.println("===TimeOut test method Two Executed===");
	}
}

Output of above test run is given below:

[TestNG] Running:
  C:\Users\Sujoy\AppData\Local\Temp\testng-eclipse-817432314\testng-customsuite.xml

===TimeOut test method Two Executed===

[Utils] Attempting to create D:\WorkSpace\DemoTestNG\test-output\Default suite\Default test.xml
[Utils]   Directory D:\WorkSpace\DemoTestNG\test-output\Default suite exists: true
PASSED: TestTwo
FAILED: TestOne

In above test methods TestOne() failed because it was not completely executed within timeout period specified.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *