TestNG engine supports a series of annotations, with these annotations it even has stronger flexibility and extensibility than Junit, we will learn these annotations from this tutorial,  first of all, let us take a quick view the life cycle of a typical TestNG case.

From the given illustration, we know that the life-cycle of a TestNG case starts with @BeforeClass and ends with @AfterClass. @BeforeClass/@AfterClass methods will be run before/after any method in a given is run, they are designed for those expensive resource initialization/cleanup and recovery, we didn’t put @BeforeSuite, @BeforeGroups, @AfterGroups and @AfterSuite to this illustration, but if they were, they will be ran even before @BeforeClass or after @AfterClass. @Configuration is deprecated so we don’t recommend use it.

TestNG Basic Annotations for configuration methods

Pri Annotation name Documentation
1 @BeforeSuite Annotates methods that will be run before any method in a given is run.
2 @BeforeGroups Annotates methods that will be run before the first method in any of the specified groups is run.
3 @BeforeClass Annotates methods that will be run before the first method on the current test class is run.
4 @BeforeTest Annotates methods that will be run before any method in a given is run.
5 @BeforeMethod Annotates methods that will be run before each test method.
6 @AfterMethod Annotates methods that will be run after every test method.
7 @AfterTest Annotates methods that will be run after all the test methods in a given have been run.
8 @AfterClass Annotates methods that will be run after the last test method on the current class is run.
9 @AfterGroups Annotates methods that will be run after the last test method belonging to the groups specified in its value attribute has been run. The annotated method is automatically put into these specified groups.
10 @AfterSuite Annotates methods that will be run after all the test methods in a given have been run.

The annotations @Test annotates a method as test case in TestNG pattern.

One example with TestNG Annotations

package com.asjava;

import org.testng.annotations.*;

public class TestNGTest {
    @BeforeGroups
    public void BeforeGroups() {
        System.out.println("@BeforeGroups");
    }

    @BeforeClass
    public void BeforeClass() {
        System.out.println("@BeforeClass");
    }

    @Test(groups = {"My group"})
    public void test1() {
        System.out.println("test1");
    }

    @Test
    public void test2() {
        System.out.println("test2");
    }

    @AfterClass
    public void AfterClass() {
        System.out.println("@AfterClass");
    }

    @AfterMethod
    public void AfterMethod() {
        System.out.println("@AfterMethod");
    }
}

Results:

[Parser] Running:
C:\Users\Administrator\.IntelliJIdea70\system\temp-testng-customsuite.xml
@BeforeClass
test1
@AfterMethod
test2
@AfterMethod
@AfterClass
===============================================
Custom suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Given by above example, the order of execution was:

@BeforeSuite->@BeforeGroups->@BeforeClass->@BeforeTest->@BeforeMethod
@AfterMethod->@AfterTest->@AfterClass->@AfterGroups->@AfterSuite