Javatpoint Logo
Javatpoint Logo

TestNG Annotation Attributes

While writing the test cases in the TestNG, you need to mention the @Test annotation before the test method.

In the above code, we have specified the @Test annotation before the test method, i.e., testcase1().

We can also explicitly specify the attributes in a @Test annotation. Test attributes are the test specific, and they are specified at the right next to the @Test annotation.

Some of the common attributes are described below:

TestNG Annotation Attributes
  • description
  • timeOut
  • priority
  • dependsOnMethods
  • enabled
  • groups

description

It is a string which is attached to the @Test annotation that describes the information about the test.

Let's understand through an example.

In the above code, we have added the description attribute in every test. The "description" attribute provides information about the test.

dependsOnMethods

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.

Let's understand through an example.

First case: When a single value is passed in a parameter.

We know that the TestNG executes the test methods in alphabetical order so, in the above program, APIStudentLogin() will execute first. However, we want WebStudentLogin() method to be executed before the execution of the APIStudentLogin() method, so this would only be possible through the "dependsOnMethods" attribute. In the above program, we have specified "dependsOnMethods" attribute in an APIStudentLogin() test method and its value is "WebStudentLogin" which means that WebStudentLogin() method will be executed before the APIStudentLogin() method.

Output

TestNG Annotation Attributes

In the above output, MobileStudentLogin() runs before the WebStudentLogin() method as TestNG runs the test methods in an alphabetical order.

Second case: When multiple values are passed in a parameter.

In the above code, testcase1() is dependent on two methods, i.e., testcase2() and testcase3(), which means that these two methods will be executed before the testcase1().

Output

TestNG Annotation Attributes

priority

When no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order. Priority determines the sequence of the execution of the test cases. The priority can hold the integer values between -5000 and 5000. When the priority is set, the lowest priority test case will run first and the highest priority test case will be executed last. Suppose we have three test cases and their priority values are -5000, 0, 15, then the order of the execution will be 0,15,5000. If priority is not specified, then the default priority will be 0.

Let's understand through an example.

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.

Output

TestNG Annotation Attributes

enabled

The 'enabled' attribute contains the boolean value. By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value.

Let's understand through an example.

In the above code, the value of the enabled attribute in jira() test method is false, so this method will not be invoked.

Output

TestNG Annotation Attributes

groups

The 'groups' attribute is used to group the different test cases that belong to the same functionality.

Let's understand through an example.

testng.xml

Output

TestNG Annotation Attributes

timeOut

If one of the test cases is taking a long time due to which other test cases are failing. To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases. The timeOut is a time period provided to the test case to completely execute its test case.

Let's understand through an example.

In the above code, inside the testcase1() method, we have Thread.sleep(500) which means that the testcase1() method will be executed after 500 milliseconds, but we have provided timeOUT attribute with the value 200 means that the testcase1() will be failed after 200 milliseconds.

testng.xml

Output

TestNG Annotation Attributes

The above screen shows that one test case is failed and other test cases are passed.

TestNG Annotation Attributes
Next TopicTestNG Parameters




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA