Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Use when working with TestNG annotations, assertions, test lifecycle, and configuration for Java testing.
allowed-tools
["Read","Write","Edit","Bash","Glob","Grep"]
TestNG Fundamentals
Master TestNG fundamentals including annotations, assertions, test lifecycle, and XML configuration for Java testing. This skill provides comprehensive coverage of essential concepts, patterns, and best practices for professional TestNG development.
Overview
TestNG is a powerful testing framework for Java inspired by JUnit and NUnit, designed to cover a wider range of test categories: unit, functional, end-to-end, and integration testing. It supports annotations, data-driven testing, parameterization, and parallel execution.
Installation and Setup
Maven Configuration
Add TestNG to your Maven project:
<dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.9.0</version>
test
import org.testng.annotations.*;
publicclassLifecycleTest {
@BeforeSuitepublicvoidbeforeSuite() {
// Runs once before the entire test suite
System.out.println("Before Suite");
}
@AfterSuitepublicvoidafterSuite() {
// Runs once after the entire test suite
System.out.println("After Suite");
}
@BeforeTestpublicvoidbeforeTest() {
// Runs before each <test> tag in testng.xml
System.out.println("Before Test");
}
@AfterTestpublicvoidafterTest() {
// Runs after each <test> tag in testng.xml
System.out.println("After Test");
}
@BeforeClasspublicvoidbeforeClass() {
// Runs once before the first test method in the class
System.out.println("Before Class");
}
@AfterClasspublicvoidafterClass() {
// Runs once after the last test method in the class
System.out.println("After Class");
}
@BeforeMethodpublicvoidbeforeMethod() {
// Runs before each test method
System.out.println("Before Method");
}
@AfterMethodpublicvoidafterMethod() {
// Runs after each test method
System.out.println("After Method");
}
@TestpublicvoidtestMethod() {
System.out.println("Test Method");
}
}
Test Annotation Attributes
The @Test annotation supports various attributes:
publicclassTestAttributesExample {
@Test(description = "Verifies user login functionality")publicvoidtestLogin() {
// Test with description
}
@Test(enabled = false)publicvoiddisabledTest() {
// This test will not run
}
@Test(priority = 1)publicvoidfirstTest() {
// Runs first (lower priority = earlier execution)
}
@Test(priority = 2)publicvoidsecondTest() {
// Runs second
}
@Test(groups = {"smoke", "regression"})publicvoidgroupedTest() {
// Test belongs to multiple groups
}
@Test(dependsOnMethods = {"testLogin"})publicvoidtestDashboard() {
// Runs only if testLogin passes
}
@Test(dependsOnGroups = {"setup"})publicvoiddependentTest() {
// Runs only if all tests in "setup" group pass
}
@Test(timeOut = 5000)publicvoidtimedTest() {
// Fails if takes more than 5 seconds
}
@Test(invocationCount = 3)publicvoidrepeatedTest() {
// Runs 3 times
}
@Test(invocationCount = 100, threadPoolSize = 10)publicvoidparallelRepeatedTest() {
// Runs 100 times across 10 threads
}
@Test(expectedExceptions = IllegalArgumentException.class)publicvoidexceptionTest() {
thrownewIllegalArgumentException("Expected");
}
@Test(expectedExceptions = RuntimeException.class,
expectedExceptionsMessageRegExp = ".*invalid.*")publicvoidexceptionWithMessageTest() {
thrownewRuntimeException("This is invalid input");
}
}