• Aurora Solutions

Annotations in TestNG

In our previous article we had learned about TestNG and how can we add packages, classes and methods to test. In this article we will focus on TestNG annotations and different features provided by them. Annotations are used to add metadata(data about data) to the existing Java source code. Annotations can be applied for classes, methods, variables and parameters which allows us to add extra information about data objects in our source code. There are certain predefine set of annotations in Java.For example, @Overide, @After, @Before, @Deprecated, @SupressWarning and so on.There are certain Annotations that TestNG has over JUnit4 one of them is extra Before and After annotations such as Before/After Suite and Before/After Group.

Before and After annotations are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends. TestNG provides five different kinds of Before and After annotation options, each of which can be used depending upon the test requirements. The following are the different before and after options provided by TestNG:

1. @BeforeSuite/@AfterSuite BeforeSuite: Method annotated with BeforeSuite will be executed before starting of any test in the suite AfterSuite: Method annotated with AfterSuite gets executed after execution all tests in the suite

2. @BeforeTest/@AfterTest BeforeTest: Method annotated with BeforeTest gets executed before the first test-method mentioned in each test inside the 'test' tag in test suite. AfterTest: Method annotated with afterTest will be executed after execution of all tests inside the 'test' tag in test suite.

3. @BeforeGroups/@AfterGroups BeforeGroup: Method annotated with BeforeGroup gets executed before executing any of the tests belonging to the group as mentioned in the 'groups' attribute. AfterGroup: Method annotated with BeforeGroup will be executed after execution of all tests belonging to the group as mentioned in the 'groups' attribute

4. @BeforeClass/@AfterClass BeforeClass: Method annotated with BeforeClass gets executed before executing any of the tests belonging to the specific class. AfterClass: Method annotated with AfterClass will be executed after execution of all tests belonging to the specific class.

5. @BeforeMethod/@AfterMethod BeforeMethod : Method annotated with BeforeMethod gets executed before each test method. AfterMethod: Method annotated with AfterMethod will be executed after execution of each test method.

we will need a sample project for defining test suites for execution.

Sample Project: a. Open Eclipse. b. create a project MutiTest . c. In MultiTest create a package firstPackage. d. In firstPackage create a class

Add the following piece of code in

public class FirstTestClass { 

public void beforeSuite(){ 
    System.out.println("Before Suite method"); 

public void afterSuite(){ 
    System.out.println("After Suite method"); 

public void beforeTest(){ 
    System.out.println("Before Test method"); 

public void afterTest(){ 
    System.out.println("After Test method"); 

public void beforeGroup(){ 
    System.out.println("Before Group Test method"); 

public void afterGroup(){ 
    System.out.println("After Group Test method"); 

public void beforeClass(){ 
    System.out.println("Before Class method"); 

public void afterClass(){ 
    System.out.println("After Class method"); 

public void beforeMethod(){ 
    System.out.println("Before Method"); 
public void afterMethod(){ 
    System.out.println("After Method"); 

public void testMethod(){ 
    System.out.println("Test one method"); 

In order to test it add the following content to the testng.xml file:

<suite name="First Suite" verbose="1" >
    <test name="First Test" >
              <class name="MultiTest.firstPackge.FirstTestClass" >
                    <include name="testMethod"/>

Execute the preceding testng.xml file as a TestNG suite.


Before Suite method
Before Test method
Before Class method
Before Group Test method
Before Method
Test one method
After Method
After Group Test method
After Class method
After Test method
After Suite method

From above example we have successfully created a test class with all kinds of Before and After annotations and executed it using a testng.xml. This also explains the sequence in which all annotations will be executed. I look forward to questions and suggestions in the comments section.

0 views0 comments

Recent Posts

See All

Aurora Holds Tech Talk At FAST-NU

Industrial seminars and workshops have always been fruitful for academia to understand the importance of specific technology and market trends of what is new and running. Such sessions provide advanta

Facebook Authentication for Spring Security in Grails

image In this article we will use Spring Security Facebook Plugin for Grails to integrate our grails application with facebook. By the end of this article you will be able to login in your grails web