TestNG – 4 || Let’s understand @Test Annotation and attributes.

Using Annotation is the way to implement additional properties in normal methods. These annotations are pre-defined and entry-point of the programs written in TestNG framework.

In the previous chapter, we have seen that keyword(annotation) @Test has been used for each method.

TestNG Annotation
TestNG Annotations

@Test Annotation

@Test Annotation is a basic and important keyword. It tells the compiler that the associated method is a Test Case in this program. The program gets executed and decides its flow of execution based on these Annotations.

Basic Attributes

Attributes have been used to set additional validation on any Annotation while execution. Annotations are like a key=value pair. The basic and mostly used attributes are mentioned below.

priority

Attribute priority=value is used to prioritize the test cases while execution. Where value should always be a numeric value.

  • @Test (priority=1)
  • @Test (priority=3)
  • @Test (priority=2)
    If priority is not set for any method, all the test cases get executed in Alphabetical Order based on the method’s name.

description

Used to add short information like “Test Case ID”, “Test Summary” etc with the associated methods. description=”String Value” can be used to achieve this.

  • @Test (priority=1, description=”TestID_001″)
  • @Test (priority=3, description=”Get User Informtion”)
  • @Test (priority=2)

enabled

There may be scenario where a particular Test Case needs to avoid while execution, but we don’t want to delete the code from the program. In such cases, enabled=value, attribute is useful.

For this attribute value should true or false based on the requirement. If the value is set to False, the associated method would not execute.

  • @Test (priority=1)
  • @Test (priority=3)
  • @Test (priority=2, enabled=false)
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 *
 */
public class basicAttributes 
{
	@Test(priority=2, description="This is sample method.")
	public void MethodB()
	{
		System.out.println("Method B called.");
	}
	
	@Test(priority=1)
	public void MethodC()
	{
		System.out.println("Method C called.");
	}
	
	
	// This method will not execute. To run this method set 'enabled=true'
	@Test(priority=4, enabled=false)
	public void MethodD()
	{
		System.out.println("Method D called.");
	}
	
	@Test(priority=3, description="TestCaseID=001")
	public void MethodA()
	{
		System.out.println("Method A called.");
	}
}
Output result
Output Result

dataProvider

This attribute is mainly used to access data and object return by @DataProvider annotation.

The drawback using ‘priority’ attribute

Although setting priority attribute to maintain execution flow is easy in short programs. But it can be a tedious job while working on a framework or lengthy projects, where hundreds of methods have been defined.

Because, if we have to change the priority of any particular method, we need to change the priority of remaining methods as well to maintain the workflow.

As a result, to overcome this TestNG introduced another attribute called “dependsOnMethods“. Let’s learn how to use this attribute in the program.

dependsOnMethods

By declaring this attribute we restrict the execution of a method based on the Pass/Fail result of another method. To understand this take below example.

@Test
public void MethodB

@Test(dependsOnMethods={“MethodB”})
public void MethodA

In the above example, if we eliminate “(dependsOnMethods={“MethodB”})” statement, execution sequence will be MethodA and then MethodB. No matter result of any Method Pass or Fail.

On the other hand, if we execute the above code along with the statement
(dependsOnMethods={“MethodB”})“. Now first MethodB will execute and if the result is Pass only then MethodA would execute.

In the below code example, we have used assertion to verify the result of a method which is under execution. Ideally, the individual method should only have one Assertion. Here, we have taken two assertions just to set the result of that method. Will discuss assert in details via next lessons.

import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 */
public class dependncyHandle 
{
	@Test
	public void login()
	{
		System.out.println("User logged in Successfully.");
	}
	
    // Hard Dependency
	@Test(dependsOnMethods= {"login"})
	public void search()
	{
		System.out.println("Search Functionalty enabled.");
		 
	           assertEquals(1, 0); // Returns result as FAILED
		// assertEquals(1, 1); // Returns result as PASSED
	}
	
	@Test(dependsOnMethods= {"search"})
	public void bookmarks()
	{
		System.out.println("Bookmark operation done.");
	}
	
    // Soft Dependency
	@Test(dependsOnMethods= {"search"},alwaysRun=true)
	public void logout()
	{
		System.out.println("User logged out successfully.");
	}	
}

Types of dependency

In the above code example, observe that we have used one more attribute “alwaysRun=true“. There are many scenarios where we need to execute dependent method even a result of the Parent method is Fail. Based on this theory there are two types of dependency present in TestNG.

Hard Dependency

In hard dependency code, alwaysRun=true would be eliminated. 
And if any test case(parent) gets failed all the dependent test cases get skipped.
@Test (dependsOnMethods={“Search”})

Soft Dependency

In hard dependency code, alwaysRun=true would be added. 
So that, all the dependent test cases get executed even parent test case gets failed.
      @Test(dependsOnMethods={“Search”}, alwaysRun=true)

alwaysRun

By default value of alwaysRun attribute remains false. As a result, we have to set it true.

These attributes can be used with other Annotations keywords as well.

Other Annotations

There are so many pre-defined annotations are present in TestNG based on the requirements.

  • Test
  • BeforeMethod and AfterMethod
  • BeforeClass and AfterClass
  • BeforeTest and AfterTest
  • BeforeGroups and AfterGroups
  • DataProvider
  • Parameters

Related Links:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Excel:


283total visits,1visits today

Leave a Reply

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