Annotation Helper Attributes in TestNG

@Test: The @Test attribute is the most important and commonly used annotation of TestNG. It is used to mark a class or a method as part of the test.
Now, let’s see some important attributes of @Test annotations
description
It is a string which is attached to the @Test annotation that describes the information about the test.
Example:
@Test(description="Sample Description")
public void description(){
System.out.println("Description of the method");
}
@Test(description="This is testcase2")
public void testcase2()
{
System.out.println("Execution of methods");
}
In the above code, we have added the description attribute in every test. The "description" attribute provides information about the test.
groups
This attribute is specifically used to group the different test cases that belong to the same functionality.
Example:
public class GroupsSample {
@Test(groups="Smoke")
public void Test(){
System.out.println("Test group :Smoke");
}
@Test(groups="Regression")
public void Test1(){
System.out.println("Test group :Regression");
}
}
In the above code, we have declared two Test method with groups attribute .So when want to execute only "Smoke Testcases" we can only run that specific group and get results.
timeOut
Here we specify the timeout value for the test method(in milliseconds) to execute. If the test takes more than the timeout value specified, the test terminates and is marked as a fail.
Example:
@Test(timeOut=200)
public void Test(){
Thread.sleep(500);
System.out.println("Executes Test");
}
In the above code, we have Thread.sleep(500) which means that the testcase method will be executed after 500 milliseconds, but we have provided timeOUT attribute with the value 200 means that the testcase will be failed after 200 milliseconds.
priority
This attribute allows to decide the execution of the method, We can schedule the execution of the method when to run.Lower priority methods execute first and priority work in descending order.When no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order.
Example:
public class Fruits
{
@Test
public void mango()
{
System.out.println("I am Mango");
}
@Test(priority=2)
public void apple()
{
System.out.println("I am Apple");
}
@Test(priority=1)
public void watermelon()
{
System.out.println("I am Watermelon");
}
}
In the above code, the default priority of mango() test method is 0, so it will be executed first. The watermelon() test method will run after mango() method as the priority of watermelon() test method is 2. The apple() test method has the highest priority, so it will be executed last.
dependsOnMethods
This attribute makes one test method dependent on another to execute.When the second test method wants to be dependent on the first test method, then this could be possible by the use of "dependOnMethods" attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run.
Example:
public class Class1
{
@Test
public void WebStudentLogin()
{
System.out.println("Student login through web");
}
@Test
public void MobileStudentLogin()
{
System.out.println("Student login through mobile");
}
@Test(dependsOnMethods= {"WebStudentLogin"})
public void APIStudentLogin()
{
System.out.println("Student login through API");
}
}
As we know Testing executes in alphabetical order, so now APIStudentLogin will execute first.But we want WebStudentLogin to execute first so this can be done only by using "dependsOnMethods" attribute.So now in above code WebStudentLogin executes first and then APIStudentLogin will be executed.
enabled
This attribute is used if you know that one of the functionality or flow is not working or there is some bug that is already reported and we are aware of that then TestNG provides a feature to safely skip that test case and execute all other test cases.By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value(Boolean Value).
Example:
@Test (enabled=false)
public void ListCart()
{
System.out.println("List of all product displayed in cart");
}
@Test
public void SearchProduct()
{
System.out.println("Search Product from List");
}
In the above code, the value of the enabled attribute in ListCart() test method is false, so this method will not be invoked.