Popular Searches
Popular Course Categories
Popular Courses

Introduction to TestNG

Introduction to TestNG

TestNG / PyTest

<div class="selenium-notes">

<h1>Introduction to TestNG</h1>

<p>TestNG is a powerful testing framework for Java that is widely used with Selenium WebDriver to organize, execute, manage, and report automated test cases. The name TestNG stands for <strong>Testing Next Generation</strong>. It provides features such as annotations, assertions, test suites, grouping, parameterization, data-driven testing, parallel execution, dependencies, and reporting.</p>

<p>In Selenium automation, TestNG acts as the test execution and test management layer. Selenium WebDriver performs browser automation, while TestNG controls when tests should run, how tests should be grouped, how test data should be supplied, and how execution results should be handled.</p>

<p>JustAcademy's Selenium Training includes TestNG as part of its automation testing curriculum, along with test annotations, assertions and validations, test execution flow, data-driven testing, and parallel test execution.</p>

<p><strong>Course Resource:</strong> <a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training</a> | <a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>

<hr>

<h2>1. What is TestNG?</h2>

<p>TestNG is an open-source Java testing framework inspired by JUnit and NUnit. It was designed to provide powerful and flexible testing capabilities for modern Java applications and automation frameworks.</p>

<p>TestNG is particularly useful in Selenium projects because it provides a structured way to create and execute automated browser test cases.</p>

<p>For example, instead of writing one large Selenium program containing login, search, cart, checkout, and logout operations, TestNG allows these scenarios to be organized as separate test methods and executed according to defined rules.</p>

<pre><code>TestNG

   |

   +-- Test Classes

   |

   +-- Test Methods

   |

   +-- Annotations

   |

   +-- Assertions

   |

   +-- Test Suites

   |

   +-- Groups

   |

   +-- Parameters

   |

   +-- Data Providers

   |

   +-- Reports

   |

   +-- Parallel Execution</code></pre>

<hr>

<h2>2. Why is TestNG Used with Selenium?</h2>

<p>Selenium WebDriver is primarily responsible for controlling browsers and interacting with web applications. It does not by itself provide a complete test management framework.</p>

<p>TestNG fills this gap by providing test organization, execution control, assertions, test dependencies, parameterization, data-driven testing, grouping, parallel execution, and reporting.</p>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Selenium WebDriver</th><th>TestNG</th></tr>

<tr><td>Controls the browser</td><td>Controls test execution</td></tr>

<tr><td>Finds web elements</td><td>Organizes test methods</td></tr>

<tr><td>Clicks buttons</td><td>Defines execution order</td></tr>

<tr><td>Enters text</td><td>Provides assertions</td></tr>

<tr><td>Handles browser navigation</td><td>Provides test annotations</td></tr>

<tr><td>Automates web application actions</td><td>Supports data-driven testing</td></tr>

<tr><td>Supports multiple browsers</td><td>Supports parallel test execution</td></tr>

<tr><td>Does not manage complete test suites</td><td>Creates and manages test suites</td></tr>

</table>

<hr>

<h2>3. TestNG Architecture in Selenium Automation</h2>

<p>A typical Selenium and TestNG automation architecture contains several layers. Selenium performs browser operations, TestNG manages test execution, and additional tools such as Maven, Git, reporting libraries, and CI/CD tools can support the overall automation framework.</p>

<pre><code>             Selenium Automation Framework

                         |

                   +-----+-----+

                   |           |

                TestNG    Selenium WebDriver

                   |           |

        +----------+-------+   |

        |          |       |   |

   Annotations Assertions Data |

        |          |       |   |

        +----------+-------+   |

                   |           |

              Test Execution  |

                   |           |

                   +-------> Browser

                              |

                       Chrome / Firefox

                       Edge / Other Browsers

                              |

                       Web Application</code></pre>

<hr>

<h2>4. Main Features of TestNG</h2>

<ul>

<li>Annotations</li>

<li>Assertions</li>

<li>Test suites</li>

<li>Test groups</li>

<li>Test dependencies</li>

<li>Parameterization</li>

<li>Data-driven testing</li>

<li>Parallel execution</li>

<li>Test execution control</li>

<li>Test prioritization</li>

<li>Listeners</li>

<li>Test reports</li>

<li>Expected exceptions</li>

<li>Timeout handling</li>

<li>Configuration methods</li>

</ul>

<hr>

<h2>5. Installing TestNG</h2>

<p>TestNG can be added to a Java Selenium project using Maven. In a Maven project, the dependency is normally added to the <strong>pom.xml</strong> file.</p>

<h3>Maven Dependency</h3>

<pre><code>&lt;dependency&gt;

    &lt;groupId&gt;org.testng&lt;/groupId&gt;

    &lt;artifactId&gt;testng&lt;/artifactId&gt;

    &lt;version&gt;7.10.2&lt;/version&gt;

    &lt;scope&gt;test&lt;/scope&gt;

&lt;/dependency&gt;</code></pre>

<p>The exact TestNG version can be updated according to the project requirements and the version supported by the project's Java and build-tool setup.</p>

<hr>

<h2>6. First TestNG Program</h2>

<p>The simplest TestNG test contains a Java class, a test method, and the <strong>@Test</strong> annotation.</p>

<pre><code>import org.testng.annotations.Test;

 

public class FirstTest {

 

    @Test

    public void verifyTitle() {

        System.out.println("TestNG test executed successfully");

    }

}</code></pre>

<p>The <strong>@Test</strong> annotation tells TestNG that the method should be treated as a test method.</p>

<hr>

<h2>7. TestNG @Test Annotation</h2>

<p>The <strong>@Test</strong> annotation is the most commonly used TestNG annotation. It marks a method as a test case.</p>

<pre><code>import org.testng.annotations.Test;

 

public class LoginTest {

 

    @Test

    public void loginTest() {

        System.out.println("Login test executed");

    }

}</code></pre>

<p>When TestNG executes this class, the method marked with <strong>@Test</strong> is recognized as a test case.</p>

<hr>

<h2>8. Important TestNG Annotations</h2>

<p>TestNG provides configuration annotations that allow setup and cleanup operations to be executed at different stages of the test lifecycle.</p>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Annotation</th><th>Purpose</th></tr>

<tr><td>@BeforeSuite</td><td>Runs before all tests in a suite</td></tr>

<tr><td>@AfterSuite</td><td>Runs after all tests in a suite</td></tr>

<tr><td>@BeforeTest</td><td>Runs before the test section defined in testng.xml</td></tr>

<tr><td>@AfterTest</td><td>Runs after the test section defined in testng.xml</td></tr>

<tr><td>@BeforeClass</td><td>Runs before the first test method in a class</td></tr>

<tr><td>@AfterClass</td><td>Runs after all test methods in a class</td></tr>

<tr><td>@BeforeMethod</td><td>Runs before each @Test method</td></tr>

<tr><td>@AfterMethod</td><td>Runs after each @Test method</td></tr>

<tr><td>@BeforeGroups</td><td>Runs before tests belonging to specified groups</td></tr>

<tr><td>@AfterGroups</td><td>Runs after tests belonging to specified groups</td></tr>

<tr><td>@Test</td><td>Defines a test method</td></tr>

</table>

<hr>

<h2>9. TestNG Execution Flow</h2>

<p>Understanding the execution lifecycle is important when building Selenium automation frameworks.</p>

<pre><code>@BeforeSuite

      |

@BeforeTest

      |

@BeforeClass

      |

@BeforeMethod

      |

   @Test

      |

@AfterMethod

      |

@AfterClass

      |

@AfterTest

      |

@AfterSuite</code></pre>

<p>The exact lifecycle becomes more important when a project contains multiple classes, suites, groups, and configuration methods.</p>

<hr>

<h2>10. Example of TestNG Annotations</h2>

<pre><code>import org.testng.annotations.*;

 

public class AnnotationExample {

 

    @BeforeSuite

    public void beforeSuite() {

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

    }

 

    @BeforeClass

    public void beforeClass() {

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

    }

 

    @BeforeMethod

    public void beforeMethod() {

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

    }

 

    @Test

    public void testLogin() {

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

    }

 

    @Test

    public void testSearch() {

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

    }

 

    @AfterMethod

    public void afterMethod() {

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

    }

 

    @AfterClass

    public void afterClass() {

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

    }

 

    @AfterSuite

    public void afterSuite() {

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

    }

}</code></pre>

<hr>

<h2>11. Assertions in TestNG</h2>

<p>Assertions are used to verify whether the actual result of a test matches the expected result. They are essential in Selenium automation because performing a browser action is not enough; the automation script must also validate the application behavior.</p>

<p>For example, after opening a login page, the test may verify the page title, URL, success message, or presence of a dashboard.</p>

<h3>Common Assertion Types</h3>

<ul>

<li>assertEquals()</li>

<li>assertNotEquals()</li>

<li>assertTrue()</li>

<li>assertFalse()</li>

<li>assertNull()</li>

<li>assertNotNull()</li>

</ul>

<h3>assertEquals Example</h3>

<pre><code>import org.testng.Assert;

import org.testng.annotations.Test;

 

public class AssertionTest {

 

    @Test

    public void verifyTitle() {

        String actualTitle = "Amazon";

        String expectedTitle = "Amazon";

 

        Assert.assertEquals(actualTitle, expectedTitle);

    }

}</code></pre>

<hr>

<h2>12. assertTrue()</h2>

<p><strong>assertTrue()</strong> verifies that a condition is true.</p>

<pre><code>Assert.assertTrue(driver.getTitle().contains("Amazon"));</code></pre>

<p>If the condition is true, the assertion passes. If the condition is false, the test fails.</p>

<hr>

<h2>13. assertFalse()</h2>

<p><strong>assertFalse()</strong> verifies that a condition is false.</p>

<pre><code>Assert.assertFalse(driver.getTitle().contains("InvalidTitle"));</code></pre>

<hr>

<h2>14. Hard Assertions</h2>

<p>TestNG's normal assertions are generally referred to as hard assertions. When a hard assertion fails, execution of the current test method stops at that assertion.</p>

<pre><code>Assert.assertEquals(actual, expected);

System.out.println("This statement will not execute if assertion fails");</code></pre>

<hr>

<h2>15. Soft Assertions</h2>

<p>Soft assertions allow multiple validations to be performed before the test reports the accumulated failures.</p>

<pre><code>import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;

 

public class SoftAssertionTest {

 

    @Test

    public void verifyMultipleValues() {

 

        SoftAssert softAssert = new SoftAssert();

 

        softAssert.assertEquals("Google", "Google");

        softAssert.assertTrue(10 &gt; 5);

        softAssert.assertEquals("Selenium", "Java");

 

        softAssert.assertAll();

    }

}</code></pre>

<p>The <strong>assertAll()</strong> call is important because it reports the collected assertion failures.</p>

<hr>

<h2>16. Selenium WebDriver with TestNG</h2>

<p>One of the most common uses of TestNG is executing Selenium WebDriver tests.</p>

<pre><code>import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

 

public class GoogleTest {

 

    @Test

    public void verifyGoogleTitle() {

 

        WebDriver driver = new ChromeDriver();

 

        driver.get("https://www.google.com");

 

        String title = driver.getTitle();

 

        Assert.assertTrue(title.contains("Google"));

 

        driver.quit();

    }

}</code></pre>

<p>In real-world frameworks, WebDriver creation and cleanup are usually moved into reusable setup classes instead of creating the driver directly inside every test method.</p>

<hr>

<h2>17. Using @BeforeMethod and @AfterMethod with Selenium</h2>

<p>A better approach is to initialize the browser before each test and close it after each test.</p>

<pre><code>import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.*;

 

public class BaseTest {

 

    protected WebDriver driver;

 

    @BeforeMethod

    public void setUp() {

        driver = new ChromeDriver();

        driver.manage().window().maximize();

    }

 

    @AfterMethod

    public void tearDown() {

        if (driver != null) {

            driver.quit();

        }

    }

}</code></pre>

<p>A test class can then extend the base class.</p>

<pre><code>import org.testng.Assert;

import org.testng.annotations.Test;

 

public class HomePageTest extends BaseTest {

 

    @Test

    public void verifyHomePage() {

 

        driver.get("https://www.google.com");

 

        Assert.assertTrue(driver.getTitle().contains("Google"));

    }

}</code></pre>

<hr>

<h2>18. Test Priority</h2>

<p>TestNG allows a priority value to be assigned to test methods.</p>

<pre><code>@Test(priority = 1)

public void loginTest() {

    System.out.println("Login");

}

 

@Test(priority = 2)

public void searchTest() {

    System.out.println("Search");

}

 

@Test(priority = 3)

public void logoutTest() {

    System.out.println("Logout");

}</code></pre>

<p>Lower priority values are generally executed before higher priority values. However, test cases should ideally remain independent rather than relying heavily on execution order.</p>

<hr>

<h2>19. Test Dependencies</h2>

<p>TestNG allows one test method to depend on another test method.</p>

<pre><code>@Test

public void login() {

    System.out.println("Login successful");

}

 

@Test(dependsOnMethods = "login")

public void checkout() {

    System.out.println("Checkout executed");

}</code></pre>

<p>Here, the checkout test depends on the login test.</p>

<p>Although dependencies are useful in some scenarios, independent test design is generally easier to maintain and parallelize.</p>

<hr>

<h2>20. Test Groups</h2>

<p>Groups allow related test cases to be categorized and executed together.</p>

<pre><code>@Test(groups = "smoke")

public void loginTest() {

    System.out.println("Login Smoke Test");

}

 

@Test(groups = "regression")

public void checkoutTest() {

    System.out.println("Checkout Regression Test");

}

 

@Test(groups = {"smoke", "regression"})

public void searchTest() {

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

}</code></pre>

<p>Groups are useful for creating smoke, sanity, regression, functional, integration, and other logical test categories.</p>

<hr>

<h2>21. Why Test Groups are Useful</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Group</th><th>Example</th></tr>

<tr><td>Smoke</td><td>Login, homepage, critical navigation</td></tr>

<tr><td>Sanity</td><td>Recently changed functionality</td></tr>

<tr><td>Regression</td><td>Complete existing functionality</td></tr>

<tr><td>Functional</td><td>Business functionality validation</td></tr>

<tr><td>Integration</td><td>Interaction between application components</td></tr>

</table>

<hr>

<h2>22. Parameterization in TestNG</h2>

<p>Parameterization allows values to be supplied to test methods from the TestNG configuration file.</p>

<h3>Java Example</h3>

<pre><code>import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class ParameterTest {

 

    @Test

    @Parameters("browser")

    public void launchBrowser(String browser) {

        System.out.println("Browser: " + browser);

    }

}</code></pre>

<h3>testng.xml</h3>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"&gt;

 

&lt;suite name="Automation Suite"&gt;

    &lt;test name="Browser Test"&gt;

        &lt;parameter name="browser" value="chrome"/&gt;

        &lt;classes&gt;

            &lt;class name="ParameterTest"/&gt;

        &lt;/classes&gt;

    &lt;/test&gt;

&lt;/suite&gt;</code></pre>

<hr>

<h2>23. Data-Driven Testing</h2>

<p>Data-driven testing means executing the same test logic with multiple sets of input data. TestNG provides the <strong>@DataProvider</strong> annotation for this purpose.</p>

<pre><code>import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

 

public class LoginDataTest {

 

    @DataProvider(name = "loginData")

    public Object[][] loginData() {

 

        return new Object[][] {

            {"user1", "password1"},

            {"user2", "password2"},

            {"user3", "password3"}

        };

    }

 

    @Test(dataProvider = "loginData")

    public void loginTest(String username, String password) {

 

        System.out.println("Username: " + username);

        System.out.println("Password: " + password);

    }

}</code></pre>

<p>The same test method is executed for each data set returned by the DataProvider.</p>

<hr>

<h2>24. DataProvider Flow</h2>

<pre><code>DataProvider

     |

     +-- Username 1 + Password 1

     |

     +-- Username 2 + Password 2

     |

     +-- Username 3 + Password 3

     |

     v

Same Test Method

     |

     v

Multiple Test Executions</code></pre>

<hr>

<h2>25. Parallel Execution</h2>

<p>TestNG can execute tests in parallel. Parallel execution can reduce overall execution time when a framework is designed correctly.</p>

<p>For example, Chrome, Firefox, and Edge tests can potentially run simultaneously using separate WebDriver instances.</p>

<pre><code>&lt;suite name="Parallel Suite" parallel="tests" thread-count="3"&gt;

    &lt;test name="Chrome Tests"&gt;

        &lt;parameter name="browser" value="chrome"/&gt;

        &lt;classes&gt;

            &lt;class name="LoginTest"/&gt;

        &lt;/classes&gt;

    &lt;/test&gt;

    &lt;test name="Firefox Tests"&gt;

        &lt;parameter name="browser" value="firefox"/&gt;

        &lt;classes&gt;

            &lt;class name="LoginTest"/&gt;

        &lt;/classes&gt;

    &lt;/test&gt;

    &lt;test name="Edge Tests"&gt;

        &lt;parameter name="browser" value="edge"/&gt;

        &lt;classes&gt;

            &lt;class name="LoginTest"/&gt;

        &lt;/classes&gt;

    &lt;/test&gt;

&lt;/suite&gt;</code></pre>

<p>Parallel execution requires thread-safe framework design. Sharing one WebDriver instance between parallel tests can cause test interference.</p>

<hr>

<h2>26. TestNG testng.xml</h2>

<p>The <strong>testng.xml</strong> file is commonly used to define test suites and control which tests, classes, methods, groups, and parameters should be executed.</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"&gt;

 

&lt;suite name="Selenium Test Suite"&gt;

    &lt;test name="Login Tests"&gt;

        &lt;classes&gt;

            &lt;class name="LoginTest"/&gt;

        &lt;/classes&gt;

    &lt;/test&gt;

&lt;/suite&gt;</code></pre>

<hr>

<h2>27. Test Suite</h2>

<p>A test suite is a collection of test cases or test classes that are executed together.</p>

<pre><code>Test Suite

   |

   +-- Login Tests

   |

   +-- Search Tests

   |

   +-- Cart Tests

   |

   +-- Checkout Tests

   |

   +-- Logout Tests</code></pre>

<p>For a large Selenium project, separate suites can be created for smoke testing, regression testing, sanity testing, browser testing, or release validation.</p>

<hr>

<h2>28. TestNG Listeners</h2>

<p>Listeners allow automation frameworks to respond to different events during test execution.</p>

<p>For example, a listener can be used to capture screenshots when a test fails, log test execution information, or integrate execution results with reporting systems.</p>

<h3>Common Listener Interfaces</h3>

<ul>

<li>ITestListener</li>

<li>ISuiteListener</li>

<li>IInvokedMethodListener</li>

<li>IRetryAnalyzer</li>

</ul>

<h3>Basic ITestListener Example</h3>

<pre><code>import org.testng.ITestListener;

import org.testng.ITestResult;

 

public class TestListener implements ITestListener {

 

    @Override

    public void onTestFailure(ITestResult result) {

        System.out.println("Test Failed: " + result.getName());

    }

 

    @Override

    public void onTestSuccess(ITestResult result) {

        System.out.println("Test Passed: " + result.getName());

    }

}</code></pre>

<hr>

<h2>29. TestNG Retry Mechanism</h2>

<p>Sometimes a test may fail because of temporary environmental or synchronization issues. TestNG supports retry mechanisms through <strong>IRetryAnalyzer</strong>.</p>

<pre><code>import org.testng.IRetryAnalyzer;

import org.testng.ITestResult;

 

public class RetryAnalyzer implements IRetryAnalyzer {

 

    private int count = 0;

    private final int maxRetry = 2;

 

    @Override

    public boolean retry(ITestResult result) {

 

        if (count &lt; maxRetry) {

            count++;

            return true;

        }

 

        return false;

    }

}</code></pre>

<p>Retry should be used carefully. It should not be used to hide genuine application defects or poorly synchronized automation.</p>

<hr>

<h2>30. Expected Exceptions</h2>

<p>TestNG allows developers to specify exceptions that are expected during a test.</p>

<pre><code>@Test(expectedExceptions = ArithmeticException.class)

public void divisionTest() {

    int result = 10 / 0;

}</code></pre>

<p>The test passes when the expected exception occurs.</p>

<hr>

<h2>31. Test Timeout</h2>

<p>TestNG allows a timeout to be specified for a test method.</p>

<pre><code>@Test(timeOut = 5000)

public void testPageLoad() {

    System.out.println("Test must complete within the configured timeout");

}</code></pre>

<p>Timeouts should be chosen carefully. Selenium synchronization should generally use appropriate waits rather than relying on arbitrary long timeouts.</p>

<hr>

<h2>32. TestNG with Page Object Model</h2>

<p>TestNG is commonly combined with the Page Object Model (POM). POM separates page-specific locators and actions from test logic.</p>

<pre><code>src/test/java

|

+-- base

|   +-- BaseTest.java

|

+-- pages

|   +-- LoginPage.java

|   +-- HomePage.java

|

+-- tests

|   +-- LoginTest.java

|

+-- utilities

|   +-- WaitUtils.java

|   +-- ScreenshotUtils.java

|

+-- testng.xml</code></pre>

<h3>LoginPage.java</h3>

<pre><code>import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

 

public class LoginPage {

 

    private WebDriver driver;

 

    private By username = By.id("username");

    private By password = By.id("password");

    private By loginButton = By.id("login");

 

    public LoginPage(WebDriver driver) {

        this.driver = driver;

    }

 

    public void enterUsername(String value) {

        driver.findElement(username).sendKeys(value);

    }

 

    public void enterPassword(String value) {

        driver.findElement(password).sendKeys(value);

    }

 

    public void clickLogin() {

        driver.findElement(loginButton).click();

    }

 

    public void login(String user, String pass) {

        enterUsername(user);

        enterPassword(pass);

        clickLogin();

    }

}</code></pre>

<h3>LoginTest.java</h3>

<pre><code>import org.testng.Assert;

import org.testng.annotations.Test;

 

public class LoginTest extends BaseTest {

 

    @Test

    public void validLoginTest() {

 

        driver.get("https://example.com/login");

 

        LoginPage loginPage = new LoginPage(driver);

 

        loginPage.login("admin", "admin123");

 

        Assert.assertTrue(

            driver.getCurrentUrl().contains("dashboard")

        );

    }

}</code></pre>

<hr>

<h2>33. TestNG with Maven</h2>

<p>Maven manages project dependencies and provides a standard mechanism for building and executing Java automation projects.</p>

<pre><code>Project

|

+-- pom.xml

|

+-- src

|   |

|   +-- main

|   |

|   +-- test

|

+-- testng.xml</code></pre>

<p>The Maven Surefire Plugin is commonly used to execute TestNG tests during Maven test execution.</p>

<h3>Example Surefire Configuration</h3>

<pre><code>&lt;plugin&gt;

    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;

    &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;

    &lt;version&gt;3.5.2&lt;/version&gt;

    &lt;configuration&gt;

        &lt;suiteXmlFiles&gt;

            &lt;suiteXmlFile&gt;testng.xml&lt;/suiteXmlFile&gt;

        &lt;/suiteXmlFiles&gt;

    &lt;/configuration&gt;

&lt;/plugin&gt;</code></pre>

<hr>

<h2>34. Running TestNG Tests</h2>

<p>TestNG tests can commonly be executed through:</p>

<ul>

<li>IDE such as IntelliJ IDEA</li>

<li>Eclipse</li>

<li>TestNG suite XML</li>

<li>Maven</li>

<li>Jenkins or another CI/CD platform</li>

<li>Command-line build processes</li>

</ul>

<hr>

<h2>35. Selenium + TestNG Execution Flow</h2>

<pre><code>Developer writes test

        |

        v

TestNG @Test method

        |

        v

@BeforeMethod

        |

        v

WebDriver initialization

        |

        v

Open application

        |

        v

Perform Selenium actions

        |

        v

Assertions / Validations

        |

        v

Test Result

   /            \

PASS            FAIL

 |                |

 v                v

Report       Screenshot/Log

        |

        v

@AfterMethod

        |

        v

Driver Quit</code></pre>

<hr>

<h2>36. TestNG Reports</h2>

<p>TestNG generates execution information that can help identify passed, failed, and skipped tests. In larger automation frameworks, TestNG results can also be integrated with reporting tools such as ExtentReports or Allure.</p>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Result</th><th>Meaning</th></tr>

<tr><td>PASS</td><td>Test completed successfully</td></tr>

<tr><td>FAIL</td><td>Test encountered a failure</td></tr>

<tr><td>SKIP</td><td>Test was not executed, often due to dependency or configuration conditions</td></tr>

</table>

<hr>

<h2>37. TestNG in a Real Automation Framework</h2>

<p>In a real project, TestNG is generally one component of a larger framework.</p>

<pre><code>                    Selenium Automation Framework

                               |

          +--------------------+--------------------+

          |                    |                    |

       TestNG              Selenium WebDriver     Maven

          |                    |                    |

    +-----+------+             |              Dependency Mgmt

    |            |             |

Annotations   Assertions      Browser

    |            |             |

DataProvider   Validation     Web Application

    |

Parallel Execution

    |

Reports / Listeners

    |

CI/CD

    |

Jenkins / Git</code></pre>

<hr>

<h2>38. Advantages of TestNG</h2>

<ul>

<li>Easy integration with Selenium WebDriver.</li>

<li>Provides powerful annotations.</li>

<li>Supports assertions and validations.</li>

<li>Supports data-driven testing.</li>

<li>Supports parameterization.</li>

<li>Supports test grouping.</li>

<li>Supports test dependencies.</li>

<li>Supports parallel test execution.</li>

<li>Supports listeners.</li>

<li>Supports retry mechanisms.</li>

<li>Supports test suites.</li>

<li>Provides execution reports.</li>

<li>Works well with Maven and CI/CD tools.</li>

<li>Helps organize large automation projects.</li>

</ul>

<hr>

<h2>39. TestNG vs JUnit</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Feature</th><th>TestNG</th><th>JUnit</th></tr>

<tr><td>Java testing framework</td><td>Yes</td><td>Yes</td></tr>

<tr><td>Annotations</td><td>Extensive configuration annotations</td><td>Extensive annotation support</td></tr>

<tr><td>DataProvider</td><td>Built-in @DataProvider</td><td>Uses parameterized testing mechanisms</td></tr>

<tr><td>Groups</td><td>Built-in grouping support</td><td>Uses tags/categories depending on version</td></tr>

<tr><td>Parallel execution</td><td>Supported</td><td>Supported through modern JUnit mechanisms and configuration</td></tr>

<tr><td>Suite XML</td><td>testng.xml</td><td>Different suite/configuration mechanisms</td></tr>

<tr><td>Dependency support</td><td>dependsOnMethods / dependsOnGroups</td><td>Different approach</td></tr>

<tr><td>Selenium usage</td><td>Very common</td><td>Also commonly used</td></tr>

</table>

<hr>

<h2>40. TestNG vs Selenium</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Selenium</th><th>TestNG</th></tr>

<tr><td>Browser automation toolset</td><td>Java testing framework</td></tr>

<tr><td>Automates browser actions</td><td>Manages test execution</td></tr>

<tr><td>Finds and interacts with elements</td><td>Validates results using assertions</td></tr>

<tr><td>Controls browser sessions</td><td>Controls test lifecycle</td></tr>

<tr><td>Does not define complete test suites</td><td>Defines test suites</td></tr>

<tr><td>Provides WebDriver APIs</td><td>Provides annotations and execution features</td></tr>

</table>

<hr>

<h2>41. Common TestNG Project Structure</h2>

<pre><code>SeleniumTestNGProject

|

+-- pom.xml

|

+-- testng.xml

|

+-- src

|   |

|   +-- test

|       |

|       +-- java

|           |

|           +-- base

|           |   +-- BaseTest.java

|           |

|           +-- pages

|           |   +-- LoginPage.java

|           |   +-- HomePage.java

|           |

|           +-- tests

|           |   +-- LoginTest.java

|           |   +-- SearchTest.java

|           |

|           +-- utilities

|               +-- WaitUtils.java

|               +-- ScreenshotUtils.java

|

+-- reports

|

+-- screenshots</code></pre>

<hr>

<h2>42. Practical Example: Login Automation</h2>

<p>Consider an e-commerce website containing a login page. The automation requirement is to validate successful login.</p>

<h3>Test Steps</h3>

<ol>

<li>Open the browser.</li>

<li>Navigate to the login page.</li>

<li>Enter username.</li>

<li>Enter password.</li>

<li>Click Login.</li>

<li>Wait for the dashboard.</li>

<li>Validate the dashboard.</li>

<li>Capture screenshot if the test fails.</li>

<li>Close the browser.</li>

</ol>

<h3>Execution Flow</h3>

<pre><code>TestNG

  |

  v

@BeforeMethod

  |

  v

Start Chrome

  |

  v

Open Login Page

  |

  v

Enter Credentials

  |

  v

Click Login

  |

  v

Dashboard

  |

  v

Assert Dashboard

  |

  +---- PASS

  |

  +---- FAIL -> Screenshot

  |

  v

@AfterMethod

  |

  v

Quit Browser</code></pre>

<hr>

<h2>43. Common Mistakes in TestNG</h2>

<h3>Mistake 1: Forgetting @Test</h3>

<pre><code>public void loginTest() {

    // TestNG may not recognize this as a test method

}</code></pre>

<p>Correct:</p>

<pre><code>@Test

public void loginTest() {

    // Test method

}</code></pre>

<h3>Mistake 2: Forgetting assertAll() with SoftAssert</h3>

<pre><code>SoftAssert softAssert = new SoftAssert();

 

softAssert.assertTrue(true);

softAssert.assertEquals("A", "B");

 

softAssert.assertAll();</code></pre>

<h3>Mistake 3: Sharing WebDriver in Parallel Tests</h3>

<p>When tests execute in parallel, each test thread should generally have its own properly managed WebDriver instance.</p>

<h3>Mistake 4: Using Test Dependencies Everywhere</h3>

<p>Excessive dependencies can make test suites fragile. Independent tests are usually easier to execute and maintain.</p>

<h3>Mistake 5: Using Priority as the Main Test Design Strategy</h3>

<p>Priority should not be used as a substitute for proper test independence and framework architecture.</p>

<hr>

<h2>44. Best Practices for TestNG</h2>

<ul>

<li>Keep test methods focused on test behavior.</li>

<li>Use meaningful test method names.</li>

<li>Use assertions for important validations.</li>

<li>Keep setup and cleanup inside appropriate configuration methods.</li>

<li>Use DataProvider for data-driven scenarios.</li>

<li>Use groups for smoke, regression, and other execution categories.</li>

<li>Keep tests independent wherever practical.</li>

<li>Use Page Object Model for maintainability.</li>

<li>Use reusable utility methods.</li>

<li>Capture screenshots for useful failure evidence.</li>

<li>Use listeners for framework-level behavior.</li>

<li>Use parallel execution only with thread-safe design.</li>

<li>Integrate TestNG execution with Maven and CI/CD.</li>

<li>Avoid hard-coded credentials and sensitive information.</li>

<li>Keep test data separate from test logic when appropriate.</li>

</ul>

<hr>

<h2>45. TestNG with CI/CD</h2>

<p>TestNG can be integrated into CI/CD workflows so that automated Selenium tests execute as part of a build or deployment pipeline.</p>

<pre><code>Developer

   |

   v

Git Push

   |

   v

Jenkins / CI Server

   |

   v

Maven Build

   |

   v

TestNG Suite

   |

   v

Selenium Tests

   |

   v

Browser Execution

   |

   v

Test Results

   |

   +---- PASS

   |

   +---- FAIL

   |

   v

Reports / Notifications</code></pre>

<p>TestNG can therefore form an important part of a larger Selenium automation workflow involving framework architecture, reporting, logging, version control, and CI/CD.</p>

<hr>

<h2>46. Real-World Use Cases of TestNG</h2>

<ul>

<li>Login testing</li>

<li>Registration testing</li>

<li>E-commerce testing</li>

<li>Shopping cart testing</li>

<li>Checkout testing</li>

<li>Search functionality testing</li>

<li>Regression testing</li>

<li>Smoke testing</li>

<li>Cross-browser testing</li>

<li>Data-driven testing</li>

<li>End-to-end testing</li>

<li>Parallel test execution</li>

<li>CI/CD automation</li>

</ul>

<hr>

<h2>47. Interview Questions on TestNG</h2>

<h3>Q1. What is TestNG?</h3>

<p>TestNG is a Java testing framework used to organize, execute, validate, and manage automated test cases. It is commonly used with Selenium WebDriver.</p>

<h3>Q2. What does TestNG stand for?</h3>

<p>TestNG stands for <strong>Testing Next Generation</strong>.</p>

<h3>Q3. What is the purpose of @Test?</h3>

<p>The @Test annotation marks a method as a TestNG test method.</p>

<h3>Q4. What is the difference between @BeforeMethod and @BeforeClass?</h3>

<p>@BeforeMethod runs before each test method, while @BeforeClass runs once before the first test method of the class.</p>

<h3>Q5. What is @AfterMethod?</h3>

<p>@AfterMethod runs after each @Test method and is commonly used for cleanup operations.</p>

<h3>Q6. What is DataProvider?</h3>

<p>DataProvider supplies multiple sets of test data to a test method and is commonly used for data-driven testing.</p>

<h3>Q7. What is testng.xml?</h3>

<p>testng.xml is a TestNG suite configuration file used to define and control test execution.</p>

<h3>Q8. What is the difference between hard and soft assertions?</h3>

<p>A hard assertion stops the current test method when it fails, whereas a SoftAssert can collect multiple failures and report them when assertAll() is called.</p>

<h3>Q9. What are TestNG groups?</h3>

<p>Groups categorize related test methods so selected categories such as smoke or regression tests can be executed together.</p>

<h3>Q10. What is parallel execution?</h3>

<p>Parallel execution means running multiple tests or test groups concurrently to reduce total execution time.</p>

<h3>Q11. What are TestNG listeners?</h3>

<p>Listeners provide hooks into test execution events and can be used for logging, screenshots, reporting, retry mechanisms, and other framework-level functionality.</p>

<h3>Q12. Can TestNG be integrated with Selenium?</h3>

<p>Yes. TestNG is commonly used with Selenium WebDriver to organize, execute, validate, and report browser automation tests.</p>

<hr>

<h2>48. Quick Revision</h2>

<table border="1" cellpadding="8" cellspacing="0">

<tr><th>Concept</th><th>Purpose</th></tr>

<tr><td>TestNG</td><td>Java test execution and management framework</td></tr>

<tr><td>@Test</td><td>Defines a test method</td></tr>

<tr><td>@BeforeMethod</td><td>Runs before each test</td></tr>

<tr><td>@AfterMethod</td><td>Runs after each test</td></tr>

<tr><td>Assert</td><td>Validates expected vs actual results</td></tr>

<tr><td>DataProvider</td><td>Supports data-driven testing</td></tr>

<tr><td>Groups</td><td>Organizes tests into categories</td></tr>

<tr><td>Priority</td><td>Controls relative execution order</td></tr>

<tr><td>Dependencies</td><td>Defines relationships between tests</td></tr>

<tr><td>testng.xml</td><td>Defines suites and execution configuration</td></tr>

<tr><td>Listener</td><td>Handles test execution events</td></tr>

<tr><td>Parallel Execution</td><td>Runs tests concurrently</td></tr>

<tr><td>Maven</td><td>Build and dependency management</td></tr>

<tr><td>Selenium</td><td>Browser automation</td></tr>

</table>

<hr>

<h2>49. Learning Roadmap: Selenium + TestNG</h2>

<pre><code>Core Java

    |

    v

Selenium WebDriver

    |

    v

Locators & WebElements

    |

    v

Browser Interactions

    |

    v

Waits & Synchronization

    |

    v

TestNG

    |

    +-- Annotations

    +-- Assertions

    +-- Groups

    +-- Parameters

    +-- DataProvider

    +-- Listeners

    +-- Parallel Execution

    |

    v

Page Object Model

    |

    v

Automation Framework

    |

    v

Maven + Git

    |

    v

Reporting

    |

    v

Jenkins / CI/CD

    |

    v

Real-Time Automation Project</code></pre>

<hr>

<h2>50. Summary</h2>

<p>TestNG is an important component of Java-based Selenium automation. Selenium WebDriver is responsible for browser automation, while TestNG provides the structure needed to organize and execute automated tests.</p>

<p>Important TestNG concepts include <strong>@Test, @BeforeMethod, @AfterMethod, assertions, test suites, groups, priorities, dependencies, parameters, DataProvider, listeners, retry mechanisms, and parallel execution</strong>.</p>

<p>When combined with Selenium WebDriver, Page Object Model, Maven, Git, reporting tools, and CI/CD systems, TestNG can become an important part of a maintainable and scalable test automation framework.</p>

<hr>

<h2>51. Course Resources</h2>

<p>Learn more about Selenium automation, WebDriver, TestNG, framework development, data-driven testing, POM, reporting, and related automation concepts through the following resources:</p>

<p><a href="https://www.justacademy.co/course-detail/selenium-training" target="_blank" rel="noopener noreferrer">Selenium Training</a></p>

<p><a href="https://www.justacademy.co/register-for-course-demo" target="_blank" rel="noopener noreferrer">Register for Course Demo</a></p>

</div>

Previous Next
whatsapp