TestNG Installation

What is TestNG?

TestNG stands for Test Next Generation. It is a powerful testing framework inspired by JUnit and NUnit. It is widely used with Selenium WebDriver and Java to organize, manage, and execute test scripts efficiently.

In the previous section, we wrote a simple automation test script. That script was designed to run a single test case. However, in real-world automation projects, the test suite contains hundreds of scripts covering numerous test scenarios. While a single test script can be executed by directly running the Java class, managing and executing an entire suite is different. This is where TestNG comes in.

TestNG brings structure and scalability to test automation. It allows test scripts to be grouped, managed, and executed as test suites. It also supports:

     Parallel execution
     Assertions for validations
     HTML report generation
     Parameterization
     Use of Listeners for advanced test behavior

For a Complete TestNG Tutorial, follow this link: https://www.qafeast.com/java-testng

For this tutorial, TestNG will be added to the project, and a basic automation script will be written using it. Detailed usage of TestNG will be covered later in the Framework Setup and Page Object Model sections.

Adding TestNG to the Maven Project

To add TestNG as a dependency in a Maven project:
     1. Search for TestNG Maven Dependency in your browser or directly go to the Maven Repository.
     2. Navigate to: Home » org.testng » testing.
     3. Choose the latest stable version and open its details page.
     4. Copy the provided Maven dependency snippet.

testng-maven-depency

Now, open your IDE:

     Open the Java project 
     Locate the pom.xml file
     Paste the copied snippet inside the block.

Note: You may need to restart your IDE or reload the Maven project to trigger dependency downloads.

Sample pom.xml with TestNG

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SeleniumDemoProject</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.31.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
</dependency>


</dependencies>

</project>

 

Adding TestNG to a Gradle Project

To add TestNG to a Gradle project:
     1. Go to the Maven Repository and search for TestNG.
     2. Navigate to Home » org.testng » testing.
     3. Select the latest stable version.
     4. On the version page, switch to the Gradle tab and copy the snippet.

Now in your IDE:

     Open the Java project 
     Open the build.gradle file.
     Paste the copied line into the dependencies block.

plugins {
id 'java'
}

group = 'org.example'
version = '1.0-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_22
targetCompatibility = JavaVersion.VERSION_22
}

repositories {
mavenCentral()
}

dependencies {
// Selenium Dependency
implementation 'org.seleniumhq.selenium:selenium-java:4.31.0'

// TestNG Dependency
testImplementation 'org.testng:testng:7.10.2'
}

test {
useTestNG()
}

 

Related Tutorials

Related Questions






Read more