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.
1 |
package TestNG; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; 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"); } @Test(groups={"Smoke","Regression"}) public void Group(){ System.out.println("Test groups execution"); } } |
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.
1 |
package TestNG; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class InvocationTimeout { @Test(priority=0) public void test(){ System.out.println("Test method"); } @Test(priority=1) public void invocationCount(){ System.out.println("Invocation Count: 5 times"); } @Test(priority=2,invocationTimeOut=50000,invocationCount=5) public void invocationTimeOut(){ System.out.println("InvocationCount:"); System.setProperty("webdriver.chrome.driver", "\\Drivers\\chromedriver.exe"); WebDriver driver=new ChromeDriver(); driver.get("http://google.co.in"); } } |
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.
1 2 3 4 |
@Test(enabled=true) public void enabled(){ System.out.println("Test enabled = true"); } |
description: The ‘description’ attribute is used to provide a description of the test method.
1 2 3 4 |
@Test(description="Sample Description") public void description(){ System.out.println("Description of the method"); } |
alwaysRun: If set to true, this test method will always be run even if it depends on a method that failed.
1 2 3 4 |
@Test(alwaysRun=true) public void alwaysRun(){ System.out.println("alwaysrun attribute"); } |
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.
1 |
package TestNG; import org.testng.annotations.Test; public class Priority { @Test public void Test(){ System.out.println("Test with default priority"); } @Test(priority=1) public void test1(){ System.out.println("Test1 with priority=1"); } @Test(priority=2) public void test2(){ System.out.println("Test2 with priority=2"); } } |
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.
1 |
package TestNG; import org.testng.annotations.Test; public class Threadpool { @Test public void test1(){ System.out.println("Test1 Method"); } @Test(invocationCount=5,threadPoolSize=3) public void invocationCount(){ long id = Thread.currentThread().getId(); System.out.println("ThreadPoolSize: Thread id is: " + id); } } |
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.
1 |
package TestNG; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class dataprovider { @DataProvider public Object[][] getData() { return new Object[][]{{2, "Two"}, {8, "Eight"}}; } @Test(dataProvider="getData") public void DPsample(int num1, String num2) { System.out.println("Dataprovider: Data(" + num1 + ", " + num2 + ")"); } } |
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.
1 |
package TestNG; import org.testng.annotations.Test; public class DPClassMain { @Test(dataProvider="DP1", dataProviderClass=DataProviderMain.class) public void Dataprovider1(Integer p) { System.out.println("DataProvider1: Data(" + p + ")"); } @Test(dataProvider="DP2", dataProviderClass=DataProviderMain.class) public void Dataprovider2(Integer p) { System.out.println("DataProvider2: Data(" + p + ")"); } } package TestNG; import org.testng.annotations.DataProvider; public class DataProviderObject { @DataProvider(name="DP1") public static Object[][] getDP1Data() { return new Object[][]{{1}}; } @DataProvider(name="DP2") public static Object[][] getDP2Data() { return new Object[][]{{2}}; } } |
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”})
1 |
package TestNG; import org.testng.annotations.Test; public class Group { @Test(groups="Smoke") public void Test2(){ System.out.println("Smoke group method"); } @Test(groups="Regression") public void Test3(){ System.out.println("Regression group method"); } @Test(dependsOnGroups = {"Smoke","Regression"}) public void DependsOnGroups(){ System.out.println("Execution of dependsOnGroups"); } } |
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.
1 |
package TestNG; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class MethodDependency { @Test public void test1() { System.out.println("test1()"); } @Test public void test2() { System.out.println("test2()"); } @Test(dependsOnMethods={"test1", "test2"}) public void DependsOnMethod() { System.out.println("DependsOnMethod()"); } } |
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.