TESTNG Annotations
• Programming annotations are labels or markers that are included in the source code as metadata to give the compiler, runtime environment, or other tools more information.
• A special syntax that uses symbols like @ is frequently followed by the name of the annotation and any associated parameters.
• Annotations can be used, among other things, to specify configuration information, document code, activate or disable particular behavior, or mark code for further processing.
Advantages of using TESTNG Annotations
• TestNG Annotations made the life of testers very easy. Based on your requirements, you can access the test methods, i.e., it has no predefined pattern or format.
• You can pass the additional parameters to TestNG annotations.
• In the case of TestNG annotations, you do not need to extend any test classes.
• TestNG Annotations are strongly typed, i.e., errors are detected at the compile time.
@Test:
• This is one of the core annotations in TestNG.
• This annotation designates a method as a test case.
• TestNG is told to run the methods as independent test cases via this annotation.
• In a class we can have more than 1 test method
@Test(enabled = false):
• You can momentarily disable a test method without deleting it from your codebase by annotating @Test(enabled = false).
• To enable again @Test(enabled = true).
Hierarchy of the TestNG Annotations:
· @BeforeSuite
· @BeforeTest
· @BeforeClass
· @BeforeMethod
· @Test
· @AfterMethod
· @AfterClass
· @AfterTest
· @AfterSuite
Types of Annotations
• @BeforeMethod - The @BeforeMethod method in TestNG will execute before each test method.
• @AfterMethod - The @AfterMethod method in TestNG will run after each test method is executed.
• @BeforeClass - The @BeforeClass method in TestNG will run before the first method invokes of the current class.
• @AfterClass - The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
• @BeforeTest -The @BeforeTest method marked with this will run once before any test method in the classes present inside the <test> tag in the TestNG XML file. It's usually used for setting up something used across many classes in the same <test> tag, like a global setting or starting shared objects.
• @AfterTest – The @AfterTest is one of the TestNG Annotations. As the name defines, @AfterTest is executed after the execution of all the @test annotated methods inside a TestNG Suite. This annotation allows developers to specify various actions to be taken after the execution of all the @test annotated methods inside a TestNG Suite.
• @BeforeSuite -The @BeforeSuite annotated method will run before the execution of all the test methods in the suite
• @AfterSuite -The @AfterSuite annotated method will run before the execution of all the test methods in the suite.
Working on TestNG Annotations
Step 1: Open the Eclipse IDE.
Step 2: Create a Maven Project.
Step 3: After Creating the Maven Project, the project exploration will look like the below image.
Step 4: Create a TestNG Class that contains all TestNG Annotations.
The below code gives you a glimpse of the Working of TestNG Annotations.
Example:
public class TC003_Annotations {
@BeforeClass
public void setup()
{
System.out.println("Before Class Execution");
}
@BeforeMethod
public void beforeMethod()
{
System.out.println("Before Method Execution");
}
@Test(priority=1,enabled=true)
public void test_case2()
{
System.out.println("TEST2");
}
@Test(priority=1,enabled=true)
public void test_case1()
{
System.out.println("TEST1");
}
@AfterMethod
public void aftermethod()
{
System.out.println("After Method Execution");
}
@AfterClass
public void teardown()
{
System.out.println("After Class Execution");
}
@AfterSuite
public void aftersuite()
{
System.out.println("After suite Test Execution");
}
@BeforeTest
public void beforetest()
{
System.out.println("Before Test Execution");
}
@AfterTest
public void aftertest()
{
System.out.println("After Test Execution");
}
@BeforeSuite
public void beforesuite()
{
System.out.println("Before suite Test Execution");
}
}
Output:
@Parameters: Indicates a method parameter’s value should be retrieved from the test suite’s XML file based on the parameter name.
Example:
@DataProvider:
· The method that provides data for parameterized testing is specified by this annotation.
· By providing several types of data, it enables you to design parameterized tests.
· we can pass different values to the TestNG testcase in a single execution
· Part of inbuilt TestNG data driven testing which is quite popular
· To get test data for your parameterized test methods, TestNG will call this function.
· For test data, the data provider method returns either a two-dimensional array or an IteratorObject[].
@Listeners:
· This annotation specifies one or more listener classes for your test suite that should be alerted of test events.
· Listeners have the option to intervene before and after specific events, such as the start of a test, its failure, and the end of the suite.
· You can create your own listeners to respond in a certain way to these occurrences.
· @Listeners can be implemented at class level and suite level
Some popular TestNG Annotation Attributes
description Attribute
It is a string which is attached to the @Test annotation that describes the information about the test.
dependsOnMethods Attribute
When the second test method wants to be dependent on the first test method, then this could be possible by the use of "dependOnMethods" attribute.
If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run.
priority Attribute
When no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order.
Priority determines the sequence of the execution of the test cases.
The priority can hold the integer values between -5000 and 5000.
When the priority is set, the lowest priority test case will run first and the highest priority test case will be executed last.
Suppose we have three test cases and their priority values are -5000, 0, 15, then the order of the execution will be 0,15,5000.
If priority is not specified, then the default priority will be 0.
groups Attribute
The 'groups' attribute is used to group the different test cases that belong to the same functionality.
timeOut Attribute
If one of the test cases is taking a long time due to which other test cases are failing.
To overcome such situation, you need to mark the test case as fail to avoid the failure of other test cases.
The timeOut is a time period provided to the test case to completely execute its test case.
Conclusion:
TestNG annotations are vital for organizing and controlling the flow of test cases in Selenium WebDriver automation.
They provide a structured approach to executing tests, allowing for setup, teardown, and configuration at various levels such as suite, test, class, and method.
This helps in creating robust and efficient test suites, enhancing the overall testing process.