INTRODUCTION
- Definition of Annotation: Annotation is a special type of metadata that provides information about the behavior of a class, method, or field in the Java programming language.
- Annotations provided by TestNG: TestNG provides several annotations that are used to configure and run tests, including @Test, @BeforeMethod, @AfterMethod, @BeforeClass, @AfterClass, and more.
- Benefits of using annotations: Annotations make it easier to write tests, as they provide a simple way to configure the behavior of test methods and classes. They also allow for more precise control over the execution of tests, making it easier to identify and fix failures.
HOW TO USE ANNOTATION
- Declare Annotation on a Class: To declare an annotation on a class, you need to add the annotation directly before the class declaration. For example, the following code declares the @Test annotation on a class:
ruby
@Test
public class ExampleTest {
// class body
}
- Declare Annotation when there is inheritance (Extends): When a class extends another class, the annotations from the parent class will be inherited by the child class. To declare an annotation in this case, simply add the annotation before the child class declaration, as in the following example:
ruby
@Test
public class ChildClass extends ParentClass {
// class body
}
- Annotation @Test: The @Test annotation is used to identify a method as a test method in TestNG. To use it, simply add the annotation before the method declaration, as in the following example:
typescript
@Test
public void testMethod() {
// method body
}
- Attributes of Annotation @Test: The @Test annotation has several attributes that can be used to configure its behavior, including:
- alwaysRun: This attribute determines whether the test method should always be run, regardless of whether it depends on a method that has failed.
- enabled: This attribute determines whether the test method should be enabled or disabled.
- description: This attribute provides a description of the test method, which can be useful for documentation purposes.
- expectedExceptions: This attribute specifies the exception that the test method is expected to throw.
- timeOut: This attribute specifies the maximum time, in milliseconds, that the test method is allowed to run.
- dataProvider: This attribute specifies the name of a data provider for the test method.
- dataProviderClass: This attribute specifies the class that provides the data for the test method.
- dependsOnGroups: This attribute specifies the groups that the test method depends on.
- dependsOnMethods: This attribute specifies the methods that the test method depends on.
- groups: This attribute specifies the groups that the test method belongs to.
Here is an example of how to use some of these attributes:
less
@Test(alwaysRun=true, description="This is a sample test method")
public void testMethod() {
// method body
}
@Test(expectedExceptions = { ArithmeticException.class }, timeOut=1000)
public void testMethodWithException() {
int x = 1 / 0;
}
5. Priority to run Test in TestNG: To specify the priority of test methods in TestNG, you can use the priority attribute of the @Test annotation. The higher the priority number, the higher the priority of the test method. For example:
less
@Test(priority=1)
public void firstTest() {
// method body
}
@Test(priority=2)
public void secondTest() {
// method body
}
The tests will be executed in the order of priority, with the test with the highest priority being executed first.
CONCLUSION
Annotations play an important role in TestNG, making it easier to write and manage tests. By using annotations, you can configure the behavior of test methods and classes, control the execution order of tests, and provide additional information about your tests. Understanding the various annotations provided by TestNG, as well as their attributes, is key to effectively using TestNG for your testing needs. Whether you are new to TestNG or have been using it for some time, taking advantage of annotations can greatly improve your testing process.