TestNG Test Annotation
The most important annotation of TestNG is Test annotation. This annotation marks a method or class as TestNG test. It you apply at class level then it will mark all the public methods present inside the class as test methods. It had lot of attributes which you can use along with the Test annotation, which will enable you to use the different features provided by the TestNG unit test framework.
Below are the list of attributes which we can use along with the Test annotation:
Attribute | Description |
---|---|
alwaysRun | Takes a true or false value. If set to true this method will always run even if it's depending method fails. |
dataProvider | The name of the data provider, which will provide data for data-driven testing to this method. |
dataProviderClass | The class where TestNG should look for the data-provider method mentioned in the dataProvider attribute. By default it's the current class or its base classes. |
dependsOnGroups | Specifies the list of groups this method depends on. |
dependsOnMethods | Specifies the list of methods this method depends on. |
description | The description of this method. |
enabled | Sets whether the said method or the methods inside the said class should be enabled for execution or not. By default its value is true. |
expectedExceptions | This attribute is used for exception testing. This attribute specifies the list of exceptions this method is expected to throw. |
groups | List of groups the said method or class belongs to. |
timeOut | This attribute is used for a time out test and specifies the time (in millisecs) this method should take to execute. |
Will learn about all these attributes in the coming blogs.
We already seen sample tests using Test annotation on methods, will see how can we use Test annotation at class level in the below example:
import org.testng.annotations.Test; @Test public class ClassLevelTestAnnotation { public void firstTest() { System.out.println("In First Test"); } public void secondTest() { System.out.println("In Second Test"); } public void thirdTest() { System.out.println("In Third Test"); } }
Run the above code using below xml configuration file:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test name="Test"> <classes> <class name="ClassLevelTestAnnotation"></class> </classes> </test> </suite>
Output for the above code is:
We can observe from the above is, we did not added the Test annotation to any of the methods in the class. Instead, we have put the Test annotation at the class level, then this will be applicable to all the public methods in the class.