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.
@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.");
}
}
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:
- TestNG – 1 || Introduction and benefits of TestNG Framework.
- TestNG – 2 || Installation process and a sample program of TestNG.
- TestNG – 3 || Create and execute multiple Test Cases.
- TestNG – 4 || Let’s understand @Test Annotation and attributes.
- TestNG – 5 || Understand Assertion in TestNG.
- TestNG – 6 || Use of @BeforeMethod and @AfterMethod.
- TestNG – 7 || Use of @BeforeClass and @AfterClass.
- TestNG – 8 || Creation and execution of Test Suites.
- TestNG – 9 || Let’s move deep into the Test Suites.
- TestNG – 10 || Use @BeforeTest and @AfterTest Annotations.
- TestNG – 11 || Groups attribute with @Test Annotation.
- TestNG – 12 || Use of @BeforeGroups & @AfterGroups.
- TestNG – 13 || Use of @BeforeSuite & @AfterSuite.
- TestNG – 14 || DataProvider annotation & attribute.
- TestNG – 15 || DataProvider with parameters.
- TestNG – 16 || Access data from Excel sheet using DataProvider.
- TestNG – 17 || Passing multiple Parameters in testng xml.
- TestNG – 18 || Multiple Browser and Parallel Execution in TestNG.
- TestNG -19 || Concept of Parallel Execution.
- TestNG – 20 || Run TestNG Program using main() method.
Java Basics:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- File Handling | Reading data from word document(.doc or .docx) in JAVA.
- File Handling | Reading data from Excel files (.xls or .xlsx) using JAVA.
- File Handling | Writing data into an Excel(.XLSX or .XLS) File.
- File Handling | Implement formatting in Excel using Java.
- File Handling | Copy existing data from one workbook to another workbook in Java.
- File Handling | Reading data from PDF file using JAVA.
- File Handling || Traverse folders and subfolders in Java.
- File Handling || Reading and Writing data from a text file.
- File Handling || Multiple file creation using template based input data.
- Framework || Simple example of Key Driven Framework using excel sheet in Selenium(JAVA).
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
- Different ways to Reverse a String in Java.
- Copy formatting & style of cells from one sheet to another.
- Getting IP address and Hostname using InetAddress Class.
- User inputs via Command Prompt using arguments of main() method of a class.
- Program for List and ArrayList in Java.
- Useful methods and implementation under Scanner Class.
- Swapping two variable values without using any third variable.
- Difference between int x= 10 and y=010 in Java.
- Parameterized Constructors v/s Setter and Getter function in JAVA.
- Override a Static Method.
- Computer Basics -13 || Creating pivot charts/tables in Excel.
- Computer Basics -12 || Advantages of PivotCharts over Simple Charts in Excel.
- Computer Basics -11 || Using Charts in place of Data Tables in Excel.
- Computer Basics -10 || How to apply Filter in a data set in Excel?
- Computer Basics -9 || Understand and Implement Data Validation in Excel.
- Computer Basics -8 || Let’s learn the usage of MS-Excel.
I rate 5 out 5 .
Thanks!!!