In the previous tutorial on TestNG, we discussed the basics of TestNG Framework and its installation steps along with the significance of having TestNG in Automation testing. Today our main agenda is all about TestNG Annotations, because when we are dealing with TestNG Framework then we must deal with TestNG Annotations.
We already know that TestNG is the ‘Next Generation’ test framework, which is inspired by Junit. TestNG Annotation plays a crucial role in designing the test cases. They are effectively used in developing data driven framework. Before we jump to the today’s agenda, let’s understand general Java annotations, after all, TestNG annotations basically uses the concept of Java annotations.
What are Java Annotations?
Java annotations are the tag which starts with ‘@’. Annotations are used to present some metadata which basically carries additional information or instruction which is ultimately used by compiler and JVM.
Annotations are used everywhere like, Methods, Classes, or Interface.
Some of the Java annotations are:
- @Override
- @SuppressWarnings
- @Deprecated
- @Retention
- @Target
- @Inherited
- @Documented
These are generic Java annotations, not TestNG Annotations. Java allows us to create own custom annotations by following some set of guidelines.
List of TestNG Annotations
You got the basic concept of Annotations. Let’s have a look at TestNG Annotations which we mainly use in designing our test cases in Automation framework. You must keep these annotations in your mind so that you could use it whenever and wherever required while writing your automation scripts.
@BeforeGroups
This annotation will be executed before invocation of the first test method of the same group.
@BeforeSuite
This annotation will run once before the start of the first test method belonging to the same test suite.
@BeforeClass
This annotation will be triggered before the first @Test annotation of the same class. It runs only once.
@BeforeMethod
A statement which is written inside this annotation will be executed before the innovation of each method with @Test annotation tag.
@Test
The statement or method written inside this annotation is the testable test case. All other TestNG annotations get invoked with respect to @Test annotation.
@AfterMethod
A statement which is written inside this annotation will be executed after the innovation of each method with @Test annotation tag.
@AfterClass
This annotation will be triggered after the execution of all the methods of the same class. It runs only once.
@AfterSuite
This annotation will run once after the execution of all the tests belonging to the same test suite.
@AfterGroups
This annotation will be executed after the invocation of the last test method of the same group.
These are immediate TestNG annotation which is used inside the test class. Let’s discuss some other annotations which help us in developing data driven framework in Selenium WebDriver.
@Parameters
This annotation helps developers to pass the short range of test data parameters to the test.
@Factory
This is another annotation which helps to set the value to the constructor. It returns Object[].
@DataProvider
This annotation makes any method as data supplier. It returns 2D object array like Object[][]. The method which takes test data from the methods which have implemented @DataProvider annotation must use dataProvider=’name of the @DataProvider annotation’.
@Listeners
This annotation helps us to implemented advanced reporting and logging in the test framework.
Recommended: WebDriver Event Listener for Effective Logging and Reporting
We will exclusively see the application of these special annotations in separate tutorials. As of now, let’s see the program which implements basic TestNG annotations.
package com.inviul.selenium.project; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeGroups; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; public class TestNGAnnotationsExample { @Test public void test() { System.out.println("This is @Test Annotion running..."); } @BeforeMethod public void beforeMethod() { System.out.println("This is @BeforeMethod Annotion running..."); } @AfterMethod public void afterMethod() { System.out.println("This is @AfterMethod Annotion running..."); } @BeforeClass public void beforeClass() { System.out.println("This is @BeforeClass Annotion running..."); } @AfterClass public void afterClass() { System.out.println("This is @AfterClass Annotion running..."); } @BeforeTest public void beforeTest() { System.out.println("This is @BeforeTest Annotion running..."); } @AfterTest public void afterTest() { System.out.println("This is @AfterTest Annotion running..."); } @BeforeSuite public void beforeSuite() { System.out.println("This is @BeforeSuite Annotion running..."); } @AfterSuite public void afterSuite() { System.out.println("This is @AfterSuite Annotion running..."); } @BeforeGroups public void beforeGroups() { System.out.println("This is @BeforeGroups Annotion running..."); } @AfterGroups public void afterGroups() { System.out.println("This is @AfterGroups Annotion running..."); } }
See the order of execution of the above program in Console screenshot below: