---
name: testng-skill
description: >
Generates TestNG tests in Java with groups, data providers, parallel execution,
XML suite configuration, and listeners. Use when user mentions "TestNG",
"@DataProvider", "testng.xml", "groups". Triggers on: "TestNG", "@DataProvider",
"testng.xml", "TestNG suite", "parallel tests Java".
languages:
- Java
category: unit-testing
license: MIT
metadata:
author: TestMu AI
version: "1.0"
---
# TestNG Testing Skill
## Core Patterns
### Basic Test with Groups
```java
import org.testng.annotations.*;
import org.testng.Assert;
public class LoginTest {
@BeforeMethod
public void setUp() { /* setup */ }
@Test(groups = "smoke")
public void testLoginSuccess() {
Assert.assertTrue(loginService.login("user@test.com", "password123"));
}
@Test(groups = "regression", dependsOnMethods = "testLoginSuccess")
public void testAccessDashboard() {
Assert.assertNotNull(dashboard.getContent());
}
@Test(expectedExceptions = AuthenticationException.class)
public void testLoginInvalidPassword() {
loginService.login("user@test.com", "wrong");
}
@AfterMethod
public void tearDown() { /* cleanup */ }
}
```
### Data Providers
```java
@DataProvider(name = "loginData")
public Object[][] loginData() {
return new Object[][] {
{"admin@test.com", "admin123", true},
{"user@test.com", "password", true},
{"invalid@test.com", "wrong", false},
};
}
@Test(dataProvider = "loginData")
public void testLogin(String email, String password, boolean expected) {
Assert.assertEquals(loginService.login(email, password), expected);
}
```
### TestNG XML Suite
```xml
```
### Parallel Execution
```xml
```
### Soft Assertions
```java
SoftAssert soft = new SoftAssert();
soft.assertEquals(user.getName(), "Alice");
soft.assertEquals(user.getAge(), 25);
soft.assertTrue(user.isActive());
soft.assertAll(); // Reports all failures at once
```
### Listeners
```java
public class TestListener implements ITestListener {
@Override public void onTestFailure(ITestResult result) {
System.out.println("Failed: " + result.getName());
// Take screenshot, log, etc.
}
}
@Listeners(TestListener.class)
public class LoginTest { /* ... */ }
```
### Lifecycle Annotations
```
@BeforeSuite → @BeforeTest → @BeforeClass → @BeforeMethod → @Test → @AfterMethod → @AfterClass → @AfterTest → @AfterSuite
```
### Anti-Patterns
| Bad | Good | Why |
|-----|------|-----|
| `dependsOnMethods` everywhere | Independent tests | Cascading failures |
| No groups | `@Test(groups = "smoke")` | Can't run subsets |
| Hard-coded test data | `@DataProvider` | Reusable |
| Priority ordering | Independent tests | Fragile |
## Quick Reference
| Task | Command |
|------|---------|
| Run suite | `mvn test -DsuiteXmlFile=testng.xml` |
| Run group | `mvn test -Dgroups=smoke` |
| Run class | `mvn test -Dtest=LoginTest` |
| Reports | `test-output/index.html` |
## Deep Patterns → `reference/playbook.md`
| § | Section | Lines |
|---|---------|-------|
| 1 | Project Setup & Configuration | Maven + Surefire config |
| 2 | Suite XML Configuration | Multi-env, parallel, groups |
| 3 | BaseTest & Thread-Safe Driver | ThreadLocal, ConfigReader |
| 4 | Data Providers (Advanced) | Excel, JSON, CSV, parallel, cross-class |
| 5 | Factory Pattern | Cross-browser matrix |
| 6 | Listeners (Production Suite) | Retry, screenshot, timing |
| 7 | Soft Assertions & Dependencies | Groups, method deps |
| 8 | Page Object Integration | PageFactory, fluent POs |
| 9 | Parallel Execution Strategies | Method/class/test/mixed |
| 10 | Reporting Integration | Allure, ExtentReports |
| 11 | CI/CD Integration | GitHub Actions, Jenkins |
| 12 | Debugging Quick-Reference | 12 common problems |
| 13 | Best Practices Checklist | 14 items |
## Source and license
Source: https://github.com/LambdaTest/agent-skills/blob/0491a3a29aa18558d2c3c64ff09367adb976c56f/testng-skill/SKILL.md
MIT License
Copyright (c) 2025 TestMu AI / LambdaTest
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.