TestNG Annotations

 

Testng is a testing framework inspired by JUnit and NUnit, designed for test configuration flexibility and powerful reporting in Java. Annotations in TestNG control how and when test methods are executed.


Common Testng Annotations

AnnotationDescription
@BeforeSuiteRuns once before all tests in the suite
@AfterSuiteRuns once after all tests in the suite
@BeforeTestRuns before <test> tag in TestNG XML file
@AfterTestRuns after <test> tag in TestNG XML file
@BeforeClassRuns before the first method in the current class
@AfterClassRuns after all methods in the current class
@BeforeMethodRuns before each @Test method
@AfterMethodRuns after each @Test method
@TestMarks a method as a test case
@DataProviderProvides data for parameterized tests
@ParametersInjects parameters from XML into test methods
@BeforeGroupsRuns before the first method of the specified group
@AfterGroupsRuns after the last method of the specified group


Example java code of Testng Annotations:

import org.testng.annotations.*;

public class TestNGAnnotationsExample {

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("→ @BeforeSuite: Runs before the entire test suite");
    }

    

    @BeforeTest
    public void beforeTest() {
        System.out.println("→ @BeforeTest: Runs before <test> section in XML");
    }



    @BeforeClass
    public void beforeClass() {
        System.out.println("→ @BeforeClass: Runs before the first method of the class");
    }

   
    @BeforeMethod
    public void beforeMethod() {
        System.out.println("→ @BeforeMethod: Runs before each @Test method");
    }

   

    @Test
    public void testCase1() {
        System.out.println("✔ Running Test Case 1");
    }

    @Test
    public void testCase2() {
        System.out.println("✔ Running Test Case 2");
    }


    @AfterMethod
    public void afterMethod() {
        System.out.println("→ @AfterMethod: Runs after each @Test method");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("→ @AfterClass: Runs after all test methods of the class");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("→ @AfterTest: Runs after <test> section in XML");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("→ @AfterSuite: Runs after the entire test suite");
    }
}


Order of Execution of TestNG Annotations

OrderAnnotationExecuted When
1@BeforeSuiteOnce before the entire test suite starts
2@BeforeTestBefore <test> tag in the XML file
3@BeforeGroupsBefore any test method belonging to a specified group
4@BeforeClassBefore the first test method in the current class
5@BeforeMethodBefore each test method (@Test)
6@TestActual test case
7@AfterMethodAfter each test method (@Test)
8@AfterClassAfter all test methods in the current class
9@AfterGroupsAfter all test methods belonging to a specified group
10@AfterTestAfter <test> tag in the XML file
11@AfterSuiteOnce after the entire test suite finishes


Execution Flow of Testng annotations

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass

@BeforeMethod
@Test
@AfterMethod

@BeforeMethod
@Test
@AfterMethod

@AfterClass
@AfterGroups
@AfterTest
@AfterSuite

No comments:

Post a Comment