Annotation attributes

Annotation attributes are used to add more information about your test methods that can be processed during the run time. We can add attributes next to the annotation. Annotations are added inside a a round bracket. It should have a name and value. You can add multiple attribute to an annotation. In the following code, we have added description attribute to the @Test annotation for method method1


Java
Copy
@Test(description = "some description")
public void method1()
{
  System.out.println("This is method 1");
}

TestNG provides several annotation attributes which has several uses during test execution. Some of the commonly used attributes are as follows:

  • description
  • priority
  • enabled
  • timeOut
  • dependsOnMethod
  • groups
  • invocationCount

Let's learn more about these annotations attributes.



description


A description is additional information about the test case. You can write any information that you would like to add about that test case, what it does or what will be the expected outcome of performing this test.


Java
Copy
    @Test(description = "This test case will validate the login functionality.")
    public void test1()
    {
        System.out.print("Login Page");
    }

priority

By default the testng run the testcase based on the alphabetical order of the name of the @Test method. But sometime you might want to run the test case in a particular order. The priority value accepts an integer. By adding priority annotation attribute, testng will begin running the test case with lowest number as priority first and then with alphabetical order where the priority equals.


Java
Copy
@Test(priority = 1)
    public void ATest()
    {
        System.out.println("priority 1");
    }
    @Test(priority = -1)
    public void BTest()
    {
        System.out.println("priority -1");
    }
    @Test(priority = 2)
    public void CTest()
    {
        System.out.println("priority 2");
    }

In the above tests, the test method with priority -1 (ie. BTest) will be executed first, followed by priority 1(ie. ATest) and priority 2 (ie. CTest).

enabled

Sometimes you may not want all the test case to be executed and want some test cases to be disabled. You ca achieve this by enabled attribute. The enabled attribute value accepts Boolean and by default it is set to true. To disable a test explicitly, you need to set its value to false.

Java
Copy
    @Test(enabled = false)
    public void testCase1()
    {
        System.out.println("This test case will not run");
    }
    @Test(enabled = true)
    public void testCase2()
    {
        System.out.println("This test case will run");
    }
    @Test
    public void testCase3()
    {
        System.out.println("This test case will run");
    }


The outcome of the above test cases are as follows:

  • The testcase with method name testCase1 will not run as we have set the attribute enabled =false.
  • The test case with method name testCase2 will run as we have set the attribute enabled = true.
  • The test case with method name testCase3 will also run as we haven't set any attribute but testng will consider the default value as enabled=true.

timeOut

You may come across a situation where certain test case might take long time to execute than the time expected. The attribution timeOut will let you set an expected time to complete the execution. It will fail the test case if the execution time exceeds the timeOut set on the test case.


Java
Copy
@Test(timeOut = 2000)
    public void testCase1()
    {
        try
        {
            Thread.sleep(5000);
            System.out.printf("This test case will fail");
        }
        catch (Exception e)
        {

        }
    }
    @Test(timeOut = 5000)
    public void testCase2()
    {
        try
        {
            Thread.sleep(2000);
            System.out.printf("This test case will pass");
        }
        catch (Exception e)
        {

        }
    }

In the above example, test method testCase2() will pass as the time taken to execute the test case is lower than the timeOut value provided in the annotation attribute. The test method testCase1() will fail and throw ThreadTimeoutException as the time taken to execute the test case is higher than the timeOut value provided in the annotation attribute.

dependsOnMethod

This attribute dependsOnMethod will restrict a test method to run only if a method given in in the value is executed and passed. If the dependsOnMethod fails, the test will be ignored. You need to provide the name of the method on which current method is depend on as string value.


Java
Copy
@Test
    public void method1()
    {
        System.out.println("This will run first");
    }
    @Test
    public void method3()
    {
        Assert.assertTrue(false);
    }
    @Test(dependsOnMethods = "method1")
    public void method2()
    {
        System.out.println("This will run if method1 is passed");
    }
    @Test(dependsOnMethods = "method3")
    public void method4()
    {
        System.out.println("This will run if method3 is passed");
    }

In the above test cases, test method method2 is depend on the method1. Hence it will run only if method1 is passed. Since in method1 there is no error, its quite sure that method2 will run. On the other hand, method4 is depend on method method3. In method3 we have added an assertion which will fail the test case and hence the method3 will fail. Since the method3 is failed the method4 which was depend on method3 will be ignored. You can add multiple method name inside the dependsOnMethod attribute value separated by comma if that method is depends on more than one method.

groups

This attribute will let you categorize or group the test cases based on your requirements. Let's say you have several modules in your application and you only want to run the test cases for only one module. In this case you can categorize all the test cases by adding groups attribute and run only required group.

Java
Copy
   @Test(groups = "group1")
    public void method1()
    {
        System.out.println("This belongs to group1");
    }
    @Test(groups = "group2")
    public void method3()
    {
        System.out.println("This belongs to group2");
    }
    @Test(groups = "group1")
    public void method2()
    {
        System.out.println("This belongs to group1");
    }
    @Test(groups = "group2")
    public void method4()
    {
        System.out.println("This belongs to group2");
    }

You can create following group node in testng.xml and run the same by including the group which you want to execute.


XML
Copy
<groups>  
	<run>  
		<include name="group1"/>  
	</run>  
</groups> 

In the above example, all the test methods having group1 as group name will be executed.

invocationCount

There might be a situation when you want to run a test case for a fixed number of time. You can achieve this by adding attribute invocationCount and providing the value as integer for number of times you want to execute.


Java
Copy
@Test(invocationCount = 5)
    public void method1()
    {
        System.out.println("This will run 5 times");
    }

In the above example the test method method1 will run for 5 time.