TestNG Installation

Below are the steps showing how you can install Testng on different popular IDEs that is Eclipse, IntelliJ IDEA, and for use with Maven/Gradle projects.


1. Installing TestNG in Eclipse

Method 1: Using Eclipse Marketplace

  • Open Eclipse.
  • Go to Help → Eclipse Marketplace.
  • In the Find box, type TestNG.
  • Click Go and find TestNG for Eclipse.
  • Click Install, accept the license, and finish.
  • Restart Eclipse.


Verify Installation:

  • Right-click on your project → New → Other → Search for TestNG Class.

  • You should also see a TestNG tab in the menu bar.


2. Installing TestNG in IntelliJ IDEA

Method 1: Enable Built-in Support

  • Open IntelliJ IDEA.
  • Go to File → Project Structure.
  • Select Modules → Dependencies.
  • Click + → Select Library → Choose From Maven.
  • Type: org.testng:testng:7.9.0
  • Click OK to download and add to your project.

Method 2: Add to Maven pom.xml

<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>7.9.0</version>
  <scope>test</scope>
</dependency>


IntelliJ will auto-download the dependency.

Verify Installation:

  • Right-click a class → Run as TestNG Test.

  • Check Run Configurations → You should see TestNG.


3. Using TestNG with Gradle Projects

Add to build.gradle:

dependencies {
    testImplementation 'org.testng:testng:7.9.0'
}

test {
    useTestNG()
}

Run:

gradle test

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