Automation Testing TestNG Guide
The complete guide to TestNG — what it is, common annotations, a Selenium example, assertions, grouping, reports, common mistakes and Selenium waits comparison.
9 programs
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →Download the TestNG guide PDF from Uncodemy: annotations, assertions, data providers, parallel runs and testng.xml with Selenium examples.
This page offers Uncodemy's TestNG guide as a PDF download. TestNG is a Java testing framework widely used with Selenium for organising, running and reporting automated tests. The guide covers the main annotations, assertions, data providers, grouping, parallel execution and the testng.xml suite file.
Three quick-access entry points into the same TestNG guide PDF: the full guide, the annotations + data provider pack, and the testng.xml + parallel execution reference. All PDFs on this page are free to download. No form is required.
The complete guide to TestNG — what it is, common annotations, a Selenium example, assertions, grouping, reports, common mistakes and Selenium waits comparison.
A worked Selenium test using @BeforeMethod, @Test and @AfterMethod, plus a @DataProvider example that runs the same test once per row of test data.
A complete testng.xml example with groups and parallel config, plus thread-safety tips (ThreadLocal drivers), reports overview and a scalable folder structure.
All three download buttons open the same TestNG guide PDF file. By downloading, you agree to our Privacy Policy. These PDFs are study material only — not a certificate or job guarantee.
TestNG is an open-source testing framework for Java, inspired by JUnit and NUnit, with extra features that suit end-to-end and integration testing. It provides annotations to mark test methods and setup steps, assertions to check results, flexible grouping and ordering, data-driven testing and built-in support for running tests in parallel. TestNG itself does not drive a browser; it is typically paired with Selenium WebDriver, which controls the browser, while TestNG structures the tests and reports the results. Many automation testing job descriptions for Java still mention TestNG, so it is worth understanding even if you later learn newer tools.
| Annotation | Runs | Typical use |
|---|---|---|
| @Test | As a test method | Marks a method as a test |
| @BeforeSuite / @AfterSuite | Once before or after the whole suite | Global setup such as reading configuration |
| @BeforeTest / @AfterTest | Before or after tests in a testng.xml test tag | Group-level setup |
| @BeforeClass / @AfterClass | Once before or after the class | Start and close a browser for the class |
| @BeforeMethod / @AfterMethod | Before or after each test method | Open a page, take screenshots on failure |
| @DataProvider | Supplies data to a test | Data-driven testing |
| @Parameters | Receives values from testng.xml | Browser name or base URL |
Illustrative structure only. It assumes Selenium and TestNG are on the project classpath and uses a public demo site; adapt locators to your application.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
public class LoginTest {
private WebDriver driver;
@BeforeMethod
public void setup() {
driver = new ChromeDriver();
driver.get("https://example.com/login");
}
@Test(groups = "smoke")
public void validateLoginShowsDashboard() {
driver.findElement(By.id("email")).sendKeys("test.user@example.com");
driver.findElement(By.id("password")).sendKeys("your-test-password");
driver.findElement(By.id("login")).click();
Assert.assertEquals(
driver.getCurrentUrl().contains("dashboard"),
true,
"User should land on the dashboard"
);
}
@AfterMethod(alwaysRun = true)
public void tearDown() {
if (driver != null) driver.quit();
}
}
| Type | Behaviour | Use when |
|---|---|---|
| Hard assert (Assert class) | Stops the test at the first failure | A later step makes no sense if this fails |
| Soft assert (SoftAssert) | Collects failures and reports them at assertAll() | Several independent checks on one page |
| assertEquals, assertTrue, assertNotNull | Common checks | Comparing values, conditions and objects |
A data provider supplies rows of data, and the test method runs once for each row.
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"valid.user@example.com", "ValidPass#1", true},
{"valid.user@example.com", "wrong-pass", false},
{"", "", false}
};
}
@Test(dataProvider = "loginData")
public void loginScenarios(String email, String password, boolean shouldSucceed) {
// enter credentials, submit, then assert the expected outcome
}
The suite file selects which tests, groups or classes run, and can set parallel execution.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="RegressionSuite" parallel="classes" thread-count="3">
<test name="Smoke">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="tests.LoginTest"/>
</classes>
</test>
</suite>
Running tests in parallel can cut execution time sharply, but only if tests do not share mutable state. A single static WebDriver shared across threads will cause tests to interfere with each other. A common approach is to store the driver in a ThreadLocal so each thread has its own browser, and to keep test data unique per test. Start with a small thread count, watch for instability and increase carefully.
| Output | Notes |
|---|---|
| Default HTML and XML reports | Generated in the output folder after every run |
| Surefire reports | Produced when TestNG runs through Maven |
| Third-party report tools | Many teams add richer HTML reports with screenshots |
| Wait type | Behaviour | Advice |
|---|---|---|
| Implicit wait | Global polling for elements to appear | Avoid mixing with explicit waits |
| Explicit wait | Waits for a specific condition on one element | Preferred for most cases |
| Fluent wait | Explicit wait with custom polling and ignored exceptions | Use for special timing cases |
| Thread.sleep | Fixed pause | Avoid; slows tests and still fails randomly |
src/main/java/pages: page objects.
src/test/java/tests: test classes.
src/test/resources: testng.xml, configuration and test data.
utils: driver factory, readers, listeners.
Ready to build TestNG frameworks that scale with parallel runs and clean reports? Explore Uncodemy's Software Testing course or book a free demo and learn from industry trainers.