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.


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.


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)


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.");
	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


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.


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.

public void 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 
	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)


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:


2 thoughts on “TestNG – 4 || Let’s understand @Test Annotation and attributes.

Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.