TestNG Attributes: While Writing the automation test script with the help of TestNG, we are marking the complete class with the @Test annotation. Because in TestNG, when we declare with @Test annotation, only each public method will be considered a test method.
We can use the @Test annotation on two levels: class level and method level. When you define it at the class level, that applies to all the class methods, but when you declare it at the method level, it is used only for that specific method.
But it needs some customization or differentiation between tests, so we can do that by adding different values to the @Test annotation, which are called attributes.
TestNG Attributes
Those attributes are specific to that @test and must be specified next to the @Test TestNG annotation. Here are some common attributes, and you can go through with those.
Here are the TestNG attributes list:
TestNG Attributes with Description
Let us discuss, one by one, with the proper description and sample programs.
Description
This Attribute will give you the information about the test to which it is attached. You can describe the following:
@Test(description = "Login Test") public void login() { System.out.println("Login Method Executed"); }
TimeOut
By using the TimeOut attribute with @Test annotation, we can define the maximum milliseconds of time to wait to complete the test execution. If the execution of that test method is not complete within that period, it will throw a TimeoutException.
For Example: “org.testng.internal.thread.ThreadTimeoutException: “ Method org.testng.internal.TestNGMethod.methodName() didn’t finish within the time-out number of seconds”.
This attribute is helpful when a test remains stuck, especially if you run your tests from a continuous delivery system, but some methods prevent the execution of the whole test suite indefinitely or take longer than expected.
Follow this link where we have discussed the TestNG TimeOut Attributes in detail.
Priority
This attribute helps the TestNG to decide the execution order. If you have not declared this attribute, the method is executed based on the method name’s sequence order, not as declared in the script.
You can read our complete post discussing the TestNG Priority.
dependsOnMethods
This attribute helps us achieve a goal, which makes the test method dependent on a particular method.
That means when you want to run a specific test, it needs to be executed only after successfully executing the depending test method.
This means we make the execution of the second test dependent on the first test’s successful execution outcome. If the first test fails, then the dependent on it will not run, and that will be marked as Skipped.
With the dependsOnMethods, we can mention a single or list of depends-on methods. We can declare the dependsOnMethods like below with a test method:
@Test(dependsOnMethods = {"Method1", "Method2". . .}) public void lofin() { }
Let us take an example to understand the TestNG dependsOnMethods attribute behavior:
package com.softwaretestingo.testng.attributes; import org.testng.Assert; import org.testng.annotations.Test; public class DependsOnMethods { @Test public void method1() { System.out.println("Method 1 Executed"); } @Test(dependsOnMethods = "method1") public void method2() { System.out.println("Method 2 Executed"); } @Test public void method3() { //Intentionally We Fail This Method Assert.fail(); System.out.println("Method 3 Executed"); } @Test (dependsOnMethods = {"method1","method3"}) public void method4() { System.out.println("Method 4 Executed"); } }
Output:
Method 1 Executed Method 2 Executed PASSED: com.softwaretestingo.testng.attributes.DependsOnMethods.method1 PASSED: com.softwaretestingo.testng.attributes.DependsOnMethods.method2 FAILED: com.softwaretestingo.testng.attributes.DependsOnMethods.method3 SKIPPED: com.softwaretestingo.testng.attributes.DependsOnMethods.method4 =============================================== Default test Tests run: 4, Failures: 1, Skips: 1 =============================================== =============================================== Default suite Total tests run: 4, Passes: 2, Failures: 1, Skips: 1 ===============================================
dependsOnGroups
Like the dependsOnMethods attribute, dependsOnGroups also behave in the same manner. With this attribute, we have to mention the group name, and the syntax looks like the below:
@Test(dependsOnGroups = {“Smoke”,”Regression”})
If any group does not execute, then the dependsOnGroups method will also not execute.
Enabled
You can decide whether a particular test method will execute using this enabled attribute of TestNG.
You can achieve this by setting the value of the enabled attribute to true or false. The default value for this enabled attribute is “True.”
When you don’t want to run some specific test method of a class at that time, you need to use the enabled attribute value to false. Those @Test methods are assigned with the ‘enabled=false,’ those methods skipped, and other methods got executed.
@Test(enabled=false) public void testMethod() { System.out.println("This is method: test1"); }
Let us see how this enabled attribute by taking a simple and easy example:
package com.softwaretestingo.testng.attributes; import org.testng.annotations.Test; public class Enabled { @Test public void method1() { System.out.println("Method 1 Executed"); } @Test(enabled = true) public void method2() { System.out.println("Method 2 Executed"); } @Test(enabled = false) public void method3() { System.out.println("Method 3 Executed"); } }
Output:
[RemoteTestNG] detected TestNG version 7.8.0 Method 1 Executed Method 2 Executed PASSED: com.softwaretestingo.testng.attributes.Enabled.method1 PASSED: com.softwaretestingo.testng.attributes.Enabled.method2 =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
Groups
By using this attribute, you can group tests together by relating their functionality of the same importance or the same type. You can use groups within TestNG to include tests with a particular group in a run or exclude them.
Please refer to the following to learn how to use TestNG Group functionality.
InvocationCount
This attribute is used with the @Test annotation to specify the number of times a method will be invoked. It is similar to the Loop functionality of Java Programming language.
By default, the invocationCount value is 1.
invocationTimeout
This attribute is used with the invocationCount attribute. With the invocationTimeout attribute, we can set the maximum time period TestNG will wait for to complete all the invocations of the test method specified in the attribute invocationCount.
package com.softwaretestingo.testng.attributes; import org.testng.annotations.Test; public class InvocationTimeout { @Test(invocationCount = 5, invocationTimeOut = 6000) public void testMethod() throws InterruptedException { System.out.println("Test Method Executed"); Thread.sleep(1000); } }
Output:
[RemoteTestNG] detected TestNG version 7.8.0 Test Method Executed Test Method Executed Test Method Executed Test Method Executed Test Method Executed PASSED: com.softwaretestingo.testng.attributes.InvocationTimeout.testMethod =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 ===============================================
Here, all the test cases passed because the execution of all the test cases was completed before the invocationTimeOut time.
But if the execution is not completed before the invocationTimeOut time, it will throw the ThreadTimeoutException.
package com.softwaretestingo.testng.attributes; import org.testng.annotations.Test; public class InvocationTimeout { @Test(invocationCount = 7, invocationTimeOut = 6000) public void testMethod() throws InterruptedException { System.out.println("Test Method Executed"); Thread.sleep(1000); } }
Output:
[RemoteTestNG] detected TestNG version 7.8.0 Test Method Executed Test Method Executed Test Method Executed Test Method Executed Test Method Executed Test Method Executed FAILED: com.softwaretestingo.testng.attributes.InvocationTimeout.testMethod org.testng.internal.thread.ThreadTimeoutException: =============================================== Default test Tests run: 1, Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Passes: 0, Failures: 1, Skips: 0 ===============================================
If you have not specified the invocationCount attribute and only specify the invocationTimeout attribute, then the invocationTimeout attribute value will be ignored.
AlwaysRun
If you set the alwaysRun attribute value to true, the test method will run even if the dependsOnMethods failed.
We have discussed the AlwaysRun attribute in detail in the following article.
ThreadPoolSize
With the help of the threadPoolSize attribute, we can define the number of threads that need to be used while running the test methods. In this way, we can run our test methods in multiple threads, and by doing this, we can reduce the test methods running time.
When we use the invocationCount attribute, the threads will be invoked multiple times, and at that time, if we set the threadPoolSize attribute based on the size of threadPoolSize, the number of threads will be used to complete the execution. The assigning of the thread is taken care of by the processor.
Example: When threadPoolSize = 1
package com.softwaretestingo.testng.attributes; import org.testng.annotations.Test; public class ThreadPoolSize { @Test(invocationCount = 5,threadPoolSize=1) public void testMethod() throws InterruptedException { System.out.printf("Thread Id : %s%n", Thread.currentThread().getId()); } }
Output:
[RemoteTestNG] detected TestNG version 7.8.0 Thread Id : 1 Thread Id : 1 Thread Id : 1 Thread Id : 1 Thread Id : 1
When threadPoolSize = 5
package com.softwaretestingo.testng.attributes; import org.testng.annotations.Test; public class ThreadPoolSize { @Test(invocationCount = 5,threadPoolSize=5) public void testMethod() throws InterruptedException { System.out.printf("Thread Id : %s%n", Thread.currentThread().getId()); } }
Output:
[RemoteTestNG] detected TestNG version 7.8.0 Thread Id : 21 Thread Id : 19 Thread Id : 22 Thread Id : 20 Thread Id : 23
DataProvider
Using this attribute, we can reuse the input data of a dataProvider for multiple test methods.
We have discussed about DataProvider in this article.
Note: this attribute is ignored if the invocationCount is not specified.
DataproviderClass
We have seen that the dataprovider is used to pass the data to a method of that class, but when the dataprovider is used to fetch the input data for the test methods from an external class at that time, we can use the dataproviderClass attribute.
The class holds the data that must be specified with the dataProviderClass attribute, and the dataprovider attribute holds the names of the methods where data must be fetched.
expectedExceptions
This attribute helps you in exception testing. With this expectedExceptions attribute, you can mention the type of exception that you are expecting during the execution of the test method.
If the exception thrown by a test method does not match the exception list entered by the user, the test method will be marked as failed.
We have discussed this expectedExceptions attribute of testNG in a separate post. To read, you can follow the link.
Final Words About TestNG Attributes
These TestNG attributes make Testing richer and more flexible. If you are using any other TestNG attributes, then you can inform us or share about those TestNG attributes in the comment section.