Adsterra Native banner 4:1

Retry Failed Test Cases in TestNG

  

In TestNG, you can retry failed test cases automatically by implementing the IRetryAnalyzer interface. This is useful when you want to re-execute flaky or intermittently failing tests a certain number of times before marking them as failed.


Steps to Retry Failed Test Cases in TestNG

  • Create a Retry Analyzer Class:

    • Implement the IRetryAnalyzer interface.

    • Override the retry() method which returns true if TestNG should retry the test.

  • Attach Retry Analyzer to Test:
    • You can attach it directly using @Test(retryAnalyzer = ...),
      or apply it globally using an IAnnotationTransformer.



Example 1: Basic Retry Analyzer with Retry Count = 3

RetryAnalyzer.java

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer {

    private int retryCount = 0;
    private static final int maxRetryCount = 3;

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            System.out.println("Retrying test: " + result.getName() + " | Attempt: " + (retryCount + 1));
            retryCount++;
            return true;
        }
        return false;
    }
}




Example Test Class: TestRetryExample.java

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestRetryExample {

    int attempt = 1;

    @Test(retryAnalyzer = RetryAnalyzer.class)
    public void testMethod() {
        System.out.println("Running test attempt: " + attempt);
        if (attempt < 3) {
            attempt++;
            Assert.fail("Failing the test intentionally");
        }
        Assert.assertTrue(true);
    }
}


Optional: Apply Retry Analyzer Globally

Instead of using @Test(retryAnalyzer = ...) on each test, you can apply the retry logic globally using IAnnotationTransformer.


RetryListener.java

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class RetryListener implements IAnnotationTransformer {
    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        annotation.setRetryAnalyzer(RetryAnalyzer.class);
    }
}



testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RetrySuite">
    <listeners>
        <listener class-name="RetryListener"/>
    </listeners>

    <test name="RetryTest">
        <classes>
            <class name="TestRetryExample"/>
        </classes>
    </test>
</suite>



Important Points:

ComponentPurpose
IRetryAnalyzerHandles retry logic for a failed test
retry()Returns true if the test should be retried
@Test(retryAnalyzer = ...)Assigns retry logic to a test
IAnnotationTransformerOptional global retry setup via listener

TestNG Parallel Execution

Parallel execution in TestNG allows you to run multiple test methods, classes, or test cases simultaneously, which reduces the execution time significantly, especially helpful for regression suites or large test sets.


Why Parallel Execution?

  • Improves execution speed

  • Optimizes resource usage

  • Useful for Cross-Browser testing or multi-user simulation


How to Do Parallel Execution in TestNG

You can do parallel execution in TestNG by configuring the testng.xml file with the parallel attribute.


Supported values for parallel attribute:

methods – Run test methods in parallel

tests – Run different <test> tags in parallel

classes – Run test classes in parallel

instances – Run multiple instances of test class in parallel


Folder Structure

/project

 ├─ /src

 │   ├─ ParallelTest1.java

 │   └─ ParallelTest2.java

 └─ testng.xml



Java Test Classes

ParallelTest1.java

import org.testng.annotations.Test;

public class ParallelTest1 {
    @Test
    public void testMethodOne() {
        System.out.println("Test Method One - Thread ID: " + Thread.currentThread().getId());
    }
}



ParallelTest2.java

import org.testng.annotations.Test;

public class ParallelTest2 {
    @Test
    public void testMethodTwo() {
        System.out.println("Test Method Two - Thread ID: " + Thread.currentThread().getId());
    }
}



testng.xml Configuration

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="ParallelTestSuite" parallel="classes" thread-count="2">
    <test name="Test1">
        <classes>
            <class name="ParallelTest1" />
            <class name="ParallelTest2" />
        </classes>
    </test>
</suite>


How it Works

  • The parallel="classes" means that ParallelTest1 and ParallelTest2 will run in parallel.

  • The thread-count="2" allows 2 threads to run simultaneously.

  • Each test method prints its Thread ID to confirm they are running in separate threads.



Output

Test Method One - Thread ID: 15
Test Method Two - Thread ID: 16


Important Points

  • Make sure your code is thread-safe (example: no shared mutable data) if you're using parallel execution.

  • Avoid using static variables unless they're synchronized or thread-local.

  • Combine with DataProviders if testing with different data in parallel.

Cross Browser Testing in TestNG

  

What is Cross-Browser Testing?

Cross-browser testing is a technique to test web applications across multiple browsers (example: Chrome, Firefox, Edge) to ensure that they work correctly and consistently for all users.


Why Cross-Browser Testing with TestNG?

TestNG allows parallel test execution across different browsers using:

  • @Parameters annotation

  • testng.xml for browser configuration

  • WebDriver for browser launching


Steps to Perform Cross-Browser Testing:

  • Add Selenium and TestNG dependencies (via Maven or manually).
  • Write a test class with @Parameters("browser").
  • Use testng.xml to define browser-specific configurations.
  • Run tests in parallel across multiple browsers (optional).


1. Add Maven Dependencies

<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.9.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.20.0</version>
    </dependency>
</dependencies>




2. Java Code for Cross-Browser Test

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.*;

public class CrossBrowserTest {

    WebDriver driver;

    @Parameters("browser")
    @BeforeMethod
    public void setup(String browserName) {
        if (browserName.equalsIgnoreCase("chrome")) {
            driver = new ChromeDriver();
        } else if (browserName.equalsIgnoreCase("firefox")) {
            driver = new FirefoxDriver();
        } else if (browserName.equalsIgnoreCase("edge")) {
            driver = new EdgeDriver();
        } else {
            throw new IllegalArgumentException("Browser not supported: " + browserName);
        }
        driver.manage().window().maximize();
    }

    @Test
    public void openGoogle() {
        driver.get("https://www.google.com");
        System.out.println("Title: " + driver.getTitle());
    }

    @AfterMethod
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}




3. testng.xml File for Cross-Browser Execution

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="CrossBrowserSuite" parallel="tests" thread-count="3">
    <test name="ChromeTest">
        <parameter name="browser" value="chrome" />
        <classes>
            <class name="CrossBrowserTest"/>
        </classes>
    </test>
    <test name="FirefoxTest">
        <parameter name="browser" value="firefox" />
        <classes>
            <class name="CrossBrowserTest"/>
        </classes>
    </test>
    <test name="EdgeTest">
        <parameter name="browser" value="edge" />
        <classes>
            <class name="CrossBrowserTest"/>
        </classes>
    </test>
</suite>


Execution:

Run the testng.xml file via:

  • Eclipse/IDEA: Right-click > Run As > TestNG Suite

  • Command Linemvn test (if configured)