0% found this document useful (0 votes)
9 views3 pages

Introduction to TestNG

TestNG is a robust testing framework for Java that enhances automation testing with features like annotations, parallel execution, and detailed reporting. It supports data-driven testing and integrates seamlessly with tools like Maven and CI/CD pipelines. The document provides installation instructions, basic examples, and XML configuration for managing test execution.

Uploaded by

raghad mustafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Introduction to TestNG

TestNG is a robust testing framework for Java that enhances automation testing with features like annotations, parallel execution, and detailed reporting. It supports data-driven testing and integrates seamlessly with tools like Maven and CI/CD pipelines. The document provides installation instructions, basic examples, and XML configuration for managing test execution.

Uploaded by

raghad mustafa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to TestNG

TestNG (Test Next Generation) is a powerful testing framework for Java that is inspired by JUnit
but provides additional functionalities, such as annotations, parallel execution, and flexible test
configurations. It is widely used for automation testing, especially with Selenium.

Why Use TestNG?


• Annotations for easy test management
• Supports parallel test execution
• Generates detailed HTML and XML reports
• Allows grouping and prioritizing test cases
• Provides data-driven testing using @DataProvider
• Integrates well with build tools like Maven and CI/CD pipelines

Installation To use TestNG in a Maven project, add the following dependency to your pom.xml:
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>

If using Eclipse, you can install TestNG from the Eclipse Marketplace.

Basic TestNG Example Create a Java class and use TestNG annotations to define test cases:
import org.testng.annotations.Test;

public class TestExample {

@Test
public void testMethod() {
System.out.println("TestNG test executed successfully!");
}
}

To run this test, right-click the file and select Run As → TestNG Test.

TestNG Annotations
• @Test: Marks a method as a test case.
• @BeforeSuite: Runs before all test cases in a suite.
• @AfterSuite: Runs after all test cases in a suite.
• @BeforeClass: Runs before any test method in the class.
• @AfterClass: Runs after all test methods in the class.
• @BeforeMethod: Runs before each test method.
• @AfterMethod: Runs after each test method.
• @DataProvider: Provides data-driven testing.

Example:
import org.testng.annotations.*;

public class AnnotationsExample {

@BeforeMethod
public void setup() {
System.out.println("Setup before each test");
}

@Test
public void test1() {
System.out.println("Executing Test 1");
}

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

@AfterMethod
public void teardown() {
System.out.println("Teardown after each test");
}
}

TestNG XML Configuration You can use a testng.xml file to define and manage test
execution:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name="TestExample"/>
</classes>
</test>
</suite>

Run it using:
mvn test

Parallel Execution Enable parallel test execution in testng.xml:


<suite name="Suite" parallel="methods" thread-count="2">
<test name="ParallelTest">
<classes>
<class name="TestExample"/>
</classes>
</test>
</suite>
Conclusion TestNG is a powerful testing framework that enhances automation testing in Java
projects. By utilizing its features, you can improve test execution efficiency and maintainability.

This guide provides a basic introduction, but you can explore more features like listeners,
assertions, and test dependencies in the official TestNG documentation at testng.org.

You might also like