Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views8 pages

What Is TestNG

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

What Is TestNG

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

SOFTWARE TESTING UNIT-4 SEMESTER-5

What is TestNG?

TestNG (Test Next Generation) is a popular testing framework for Java applications that facilitates the efficient
and organized execution of test cases. One of its key features is the ability to parameterize tests, allowing
developers to run the same test method with different sets of data.

 Powerful Testing Framework: TestNG is a popular open-source framework that gives you a lot of
control over how you write and run your automated tests.

 Successor to JUnit: Built upon the foundation of JUnit, TestNG offers additional features and a more
user-friendly experience.

 Easy Test Control: TestNG allows you to easily define test case dependencies. You can ensure that
specific tests run before others, mimicking real-world test workflows.

 Enhanced Functionality: Through annotations, grouping, test sequencing, and data-driven testing,
TestNG provides more powerful and flexible ways to design your test suites.

TestNG Framework:
TestNG, short for Test Next Generation, is a powerful and flexible testing framework designed for Java
applications. It is inspired by JUnit and NUnit but introduces enhanced functionalities that make it
more robust and easier to use. TestNG supports a wide range of testing types, including unit,
functional, integration, and end-to-end testing, making it a popular choice for enterprise-level
applications.

Key Features of TestNG

TestNG offers several features that simplify the process of writing and executing tests:

 Annotations: TestNG uses annotations like @Test, @BeforeSuite, @AfterTest, etc., to define test
methods and control their execution flow. These annotations provide better flexibility and readability
compared to older frameworks.

 Test Dependencies: It allows you to define dependencies between tests, ensuring that certain tests
run only after others have successfully executed.

 Data-Driven Testing: With features like @DataProvider, TestNG supports parameterized testing,
enabling the same test method to run with multiple sets of data.

 Grouping and Prioritization: You can group related tests and assign priorities to control the order of
execution.

 Parallel Execution: TestNG supports running tests in parallel, which is particularly useful for cross-
browser testing and improving execution speed.

 Detailed Reports: It generates comprehensive HTML and XML reports, making it easier to analyze test
results.

Advantages of TestNG

TestNG overcomes many limitations of older frameworks like JUnit. Some of its advantages include:

 Ease of Use: Its annotations and configuration options make it user-friendly and efficient for testers1.

1|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

 Flexibility: It supports advanced features like retrying failed tests, custom listeners, and integration
with tools like Selenium3.

Scalability: TestNG is suitable for small projects as well as large, complex test suites

Example of TestNG Annotations

Here’s a simple example demonstrating the use of TestNG annotations:

import org.testng.annotations.*;

public class TestNGExample {

@BeforeSuite

public void beforeSuite() {

System.out.println("Before Suite");

@BeforeTest

public void beforeTest() {

System.out.println("Before Test");

@Test(priority = 1)

public void testCase1() {

System.out.println("Executing Test Case 1");

@Test(priority = 2)

public void testCase2() {

System.out.println("Executing Test Case 2");

@AfterTest

public void afterTest() {

System.out.println("After Test");

2|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

@AfterSuite

public void afterSuite() {

System.out.println("After Suite");

Output:

Before Suite

Before Test

Executing Test Case 1

Executing Test Case 2

After Test

After Suite

Practical Applications

TestNG is widely used in automation testing with tools like Selenium. It is ideal for scenarios requiring cross-
browser testing, data-driven testing, and parallel execution. Its ability to handle complex test workflows
makes it a preferred choice for QA engineers

TestNG Annotations and Listeners


TestNG is a powerful testing framework that provides annotations and listeners to control the flow of
test execution and customize test behavior. These features enhance test automation by offering
flexibility, better reporting, and logging capabilities.
TestNG Annotations
Annotations in TestNG define the sequence and behavior of test methods. They are executed in a
specific order, making it easier to manage test workflows. Below are some commonly used
annotations:
 @BeforeSuite: Executes before all tests in the suite.
 @BeforeTest: Executes before each <test> section in the TestNG XML file.
 @BeforeClass: Executes before the first test method in the current class.
 @BeforeMethod: Executes before each test method.
 @Test: Marks a method as a test case.
 @AfterMethod: Executes after each test method.

3|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

 @AfterClass: Executes after all test methods in the current class.


 @AfterTest: Executes after each <test> section.
 @AfterSuite: Executes after all tests in the suite.
Example of Annotation Workflow
The execution order is as follows:
@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod ->
@AfterClass -> @AfterTest -> @AfterSuite
Attributes of @Test
 alwaysRun: Ensures the method runs even if dependent methods fail.
 dataProvider: Supplies data for data-driven testing.
 dependsOnMethods: Specifies dependencies on other methods.
 priority: Sets the execution order of test methods.
 timeout: Fails the test if it exceeds the specified time limit.
Example
@Test(priority = 1)
public void loginTest() {
System.out.println("Login successful");
}

@Test(priority = 2, dependsOnMethods = {"loginTest"})


public void dashboardTest() {
System.out.println("Dashboard loaded");
}
TestNG Listeners
Listeners in TestNG are interfaces that allow you to hook into the test execution process. They
"listen" to events during test execution and perform actions like logging, reporting, or taking
screenshots.
Commonly Used Listeners
1. ITestListener: Monitors test-level events such as start, success, failure, or skip.
2. ISuiteListener: Monitors suite-level events like start and finish.
3. IExecutionListener: Tracks the start and end of the entire TestNG execution.
4|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

4. IReporter: Generates custom reports after test execution.


5. IInvokedMethodListener: Executes before and after each method invocation.
Example of ITestListener
import org.testng.ITestListener;
import org.testng.ITestResult;

public class CustomListener implements ITestListener {


@Override
public void onTestStart(ITestResult result) {
System.out.println("Test started: " + result.getName());
}

@Override
public void onTestSuccess(ITestResult result) {
System.out.println("Test passed: " + result.getName());
}

@Override
public void onTestFailure(ITestResult result) {
System.out.println("Test failed: " + result.getName());
}
}
Applying Listeners
Listeners can be applied in two ways:
 Using @Listeners Annotation:
@Listeners(CustomListener.class)
public class TestClass {
@Test
public void sampleTest() {
System.out.println("Executing test");
5|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

}
}
 Using testng.xml:
<listeners>
<listener class-name="CustomListener" />
</listeners>
Benefits of Listeners
 Enhanced Reporting: Capture detailed logs and generate custom reports.
 Debugging: Take screenshots or log additional information on test failures.
 Dynamic Test Control: Modify test execution flow based on runtime conditions.
TestNG Example
Sure! Here's a basic but complete TestNG example in Java, which you can use to explain in your class.
It includes the following TestNG concepts:

 @BeforeClass
 @AfterClass
 @Test
 Assertions
 TestNG XML (optional, for running tests)
Java Class with TestNG Example
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class CalculatorTest {

Calculator calc;

// This method runs once before any test


@BeforeClass

6|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

public void setUp() {


calc = new Calculator();
System.out.println("Setup: Calculator object created");
}

// Test for addition


@Test
public void testAddition() {
int result = calc.add(5, 3);
Assert.assertEquals(result, 8, "Addition result is incorrect");
}

// Test for subtraction


@Test
public void testSubtraction() {
int result = calc.subtract(10, 4);
Assert.assertEquals(result, 6, "Subtraction result is incorrect");
}

// This method runs once after all tests


@AfterClass
public void tearDown() {
System.out.println("Teardown: Tests completed");
}
}
Simple Calculator in class
public class Calculator {
public int add(int a, int b) {
return a + b;
}
7|RSR
SOFTWARE TESTING UNIT-4 SEMESTER-5

public int subtract(int a, int b) {


return a - b;
}
}
TestNG XML File (Optional)
Create a file called testng.xml if you want to run the tests via XML configuration.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="Calculator Suite">
<test name="Calculator Test">
<classes>
<class name="CalculatorTest"/>
</classes>
</test>
</suite>

✅ Output When Running the Test


Setup: Calculator object created
PASSED: testAddition
PASSED: testSubtraction
Teardown: Tests completed

8|RSR

You might also like