Home > Automation Testing > Advance Selenium > TestNG Test Annotation with attributes and sample code

TestNG Test Annotation with attributes and sample code

This post was most recently updated on August 5th, 2024

Test Annotation with attributes in detail with example.

@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.
We write code/business logic in the @Test method. If we want to automate something, that particular code needs to be written into the test method.
It’s easier to maintain dependency using @Test.

@Test public void Test{ System.out.println(“Test method”); }

Now, let’s see some important attributes of @Test annotations-

groups: This attribute is specifically used to specify the groups.

In the above code, we have declared two Test method with groups attribute and one Group method where we are calling these methods in the Group method. Group method will execute as per the group’s declaration.

timeout: In this attribute, 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. By default, the value of the timeout attribute is Zero.

@Test(timeOut=500) public void Test(){ System.out.println(“Executes Test”); }

In the above example, If the test method executes in the given timeout then it will successfully execute otherwise it will give an error.

invocationCount: This attribute is used to specify the number of times this method will invoke.
The default value for invocationCount is 1.

package TestNG; import org.testng.annotations.Test; public class InvocationCount { @Test(invocationCount=5) public void InvocationCounts(){ System.out.println(“InvocationCounts”); } @Test public void Test(){ System.out.println(“Test”); } }

In the above output invocationCounts methods will execute 5 times as we declared invocation =5 attribute. So invocation method will execute depending upon the invocationCount attribute count declared in the method.

invocationTimeout: In this attribute, the maximum number of milliseconds this invocationTimeout will execute for every invocationCount. This attribute will be ignored if invocationCount is not specified.

In the above code, InvocationCounts method will execute 5 times as we declared invocationCount=5 and invocationTimeout attribute will execute for every invocationCount and will check for all methods if method executes within 50 seconds for not it doesn’t then will throw an error.

enabled: This attribute is used to specify whether the given test method will run with the suite or class or not.

description: The ‘description’ attribute is used to provide a description of the test method.

alwaysRun: If set to true, this test method will always be run even if it depends on a method that failed.

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. The default value of the priority is 0.

In the above result, we can see the methods executed as per the priority initialized.

threadPoolSize: In this method, the thread will be invoked multiple times depending on the invocationCount.
Note: this attribute is ignored if invocationCount is not specified.

In the above result, the thread is executing 5 times because we have assigned invocationCount=5 so the method will run 5 times and the thread will execute 3 times, but we can’t tell the execution of the thread which method will run. The assigning of the thread is taken care by the processor.

dataprovider: With this attribute, we can reuse the input data from a DataProvider for multiple test methods.

dataproviderClass: In this attribute, dataprovider is used to fetch the input data for the test methods from an external class. The class which holds the data is set to be a dataProviderClass attribute and dataProvider holds the name of the method where data need to be fetched.

First, we have created a DataProviderObject class which contains the @DataProvider. Now we created one more class DPClassMain in which we are going to call the @dataProvider and @dataProviderClass attributes. Next in DPClassMain will call the dataProvider and dataProviderClass which will fetch the dataProvider from DataProviderObject class.

dependsOnGroups: In this attribute the list of groups this method depends upon.
@Test(dependsOnGroups = {“Smoke”,”Regression”})

In the above result, we can see the execution of the DependsOnGroups method depends on the list of the groups listed. If any group doesn’t execute then DependsOnGroups method will also not execute.

dependsOnMethods: In this attribute, the dependency of the method depends on the list of methods.

In the above result, we can see the dependency of the method DependsOnMethod depends on the list of methods. If any method fails then DependsOnMethod will not execute.

In the next topic, We will cover Before and After annotations in TestNG with sample code. 

 

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Devanand Tripathi
Software Test Engineer Skills : Selenium Webdriver, Manual Testing, Database Testing, STLC Java, UI / UX Testing

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">