TestNG Installation




What is TestNG?

TestNG (NG stands for "Next Generation") is an open-source testing framework designed to simplify a broad range of testing needs, from unit testing to integrated and end-to-end testing, particularly for the Java ecosystem.

TestNG's as a tool centers on providing a powerful, flexible, and structured environment for running tests. It addresses the limitations of older frameworks by offering features that make writing and managing complex test scenarios easier.


TestNG Installation

The installation process for TestNG framework is essentially about establishing the necessary dependencies and configuration within a project environment. Since TestNG is primarily used with the Java ecosystem, its installation theory centers on the integration with development tools.

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.












Below are methods used for installation of TestNG framework for Eclipse and Intelli J Idea IDEs.

Please have look for each of the method and install in your machine.


1. Installing TestNG in Eclipse


Method a: 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 a: 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 b: 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



Suggested Posts:

1. Read Excel Data by Data provider
2. TestNG Dependent Tests
3. TestNG Reports
4. Parameterization in TestNG
5. Priority in TestNG

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.

TestNG annotations are special markers that you use to logically structure and control the execution flow of your tests. They tell the framework what a particular method is for and when it should be run relative to other tests, groups, classes, or the entire test suite.











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:

Below is the TestNG test class contains all annotations as mentioned above. Statement will be printed on console on execution of particular annotation. @BeforeMethod and @AfterMethod will execute with each test associated with @Test. 

Below in table order of annotations is also mentioned, that in which order annotations will be executed.
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

Below is the flow of TestNG annotations, we can see @BeforeMethod and
@AfterMethod executed with each Test associated with @Test annotation. Since we
have, two test cases which are annotated with @Test so @BeforeMethod and
@AfterMethod executed two times.

@BeforeSuite
@BeforeTest
@BeforeGroups
@BeforeClass

@BeforeMethod
@Test
@AfterMethod

@BeforeMethod
@Test
@AfterMethod

@AfterClass
@AfterGroups
@AfterTest
@AfterSuite



Suggested Posts:

1. First TestNG Script
2. TestNG Dependent Tests
3. TestNG Reports
4. Parameterization in TestNG
5. TestNG Reporter Log