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, then only each public method will be considered as a test method.
We can use the @Test annotation in two-level one is class level, and the other one is method level. When you define at class level, then that applies to all the methods of that class, but when you declare at the method level, then that is used to that specific method only.
But it needs some customization or differentiation between testes, so we can do that by adding different values to the @Test annotation, and those values are called attributes.
TestNG Attributes
Those attributes are specific to that @test and need to be specified right next to the @Test TestNG annotation. Below we are trying to share some of the common attributes, and you can go through with those. Here are the TestNG attributes list:
- Description
- timeOut
- Priority
- dependsOnMethods
- enabled
- groups
- invocationCount
- invocationTimeout
- alwaysRun
- threadPoolSize
- dataProvider
- dataproviderClass
TestNG Attributes with Description
Let us discuss one by one, with the proper description and sample programs.
1. Description: This is a String that gives the information about the test it is attached to. you can describe like below:
@Test(description="This is testcase1 desc") public void test1() { System.out.println("This is method: test1"); }
2. TimeOut: By using this, we can define maximum milliseconds to a test run. Until the execution of that test method is not complete within that period then it will throw a TimeoutException like this “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 the test remains stuck, especially if you run then from a continuous delivery system, but some method preventing the execution of the whole test suite indefinitely.
3. Priority: If you have not declared this attribute, then the method is executed based on the method names sequence order, not as they are declared in the script. For more information, you can read your complete post about the priority in TestNG.
4. dependsOnMethods: When you want to run a specific test needs to be executed only after other tests executed successfully, which means we make the execution of the second test is dependent on the first tests successfully outcome. So to handle such type of scenario, you can use this attribute in your script. If the first test got failed, then the dependent on it will not run, and that will be marked as Skipped.
5. Enabled: You can assign any Boolean value to this attribute, and the default value for this attribute is “True.” These attributes help you when you don’t want to run some specific tests of a class to be run by assign the false value to the attribute like below. 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"); }
6. Groups: By using this attribute, you can able to grouping together of tests by relating their functionality, of the same importance or same type. You can use groups from within TestNG to either include tests having a particular group in a run or to exclude them. To learn how to user this functionality, please refer to:
I hope after reading this article, you have to get an idea of how to use those attributes with the @Test annotation. If you have any suggestions/tips, then you can inform by a comment in the comment section, and we try to take the necessary action to make it more helpful for our readers.
7. InvocationCount: This attribute is used with the @Test annotation to specify the number of times a method will invoke. By Default, the invocationCount value is 1.
8. invocationTimeout: This attribute is used with the invocationCount attribute. With the invocationTimeout attribute, we can set a maximum number of milliseconds for every invocationCount execute. If you have not specified the invocationCount attribute and only specifies the invocationTimeout attribute then the invocationTimeout attribute value will be ignored.
9. AlwaysRun: If you set the alwaysRun attribute value to true then the test method will be run even if the depends on method got failed.
dependsOnGroups: Like dependsOnMethods attribute dependsOnGroups also behave in the same manner. With this attribute we have to mention the group name and the syntax looks like below:
@Test(dependsOnGroups = {“Smoke”,”Regression”})
If any group does not execute then the dependsOnGroups method will also not execute.
10. ThreadPoolSize: When we use the invocationCount then that time the threads will be invoked multiple times and at that time if we set the threadPoolSize attribute that that thread will be executed for that mentioned time. The assigning of the thread is taken care of by the processor.
11. DataProvider: By using this attribute, we can reuse the input data of a dataProvider for multiple test methods.
Note: this attribute is ignored if the invocationCount is not specified.
12. 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 which holds the data that need to be specified with the dataProviderClass attribute and the dataprovider attributes holds the name of the methods where data need to be fetched.
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.
Source: Links
Leave a Reply