Comprehensive testing framework for Java with annotations, data-driven testing, and parallel execution capabilities
npx @tessl/cli install tessl/maven-org-testng--testng@7.11.0TestNG is a comprehensive testing framework for Java that provides powerful testing capabilities including data-driven testing, parallel execution, test configuration through annotations, flexible test configuration through XML files, and integration with build tools and IDEs. It supports advanced features like test dependencies, parameterized tests, groups, test listeners, and reporters.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
public class BasicTestExample {
@BeforeMethod
public void setUp() {
// Setup code before each test method
System.out.println("Setting up test");
}
@Test
public void testSimpleAssertion() {
String expected = "Hello TestNG";
String actual = "Hello TestNG";
Assert.assertEquals(actual, expected);
}
@Test(groups = "unit")
public void testWithGroup() {
Assert.assertTrue(true, "This should pass");
}
@Test(dataProvider = "testData")
public void testWithDataProvider(String input, int expected) {
Assert.assertEquals(input.length(), expected);
}
@DataProvider(name = "testData")
public Object[][] createTestData() {
return new Object[][] {
{"Hello", 5},
{"TestNG", 6},
{"Framework", 9}
};
}
}TestNG is built around several key components:
Core annotations for marking and configuring test methods and classes. The @Test annotation is the primary way to mark methods as tests, with extensive configuration options for groups, dependencies, timeouts, and data providers.
@Test(
groups = {"unit", "integration"},
dependsOnMethods = {"setUp"},
timeOut = 5000,
dataProvider = "testData",
enabled = true,
priority = 1
)
public void testMethod() { }
@DataProvider(name = "testData", parallel = true)
public Object[][] provideTestData() { }Comprehensive assertion library providing static methods for test verification. Supports basic assertions, object comparison, array/collection comparison, and exception testing with detailed failure messages.
public class Assert {
public static void assertEquals(Object actual, Object expected);
public static void assertTrue(boolean condition, String message);
public static void assertNotNull(Object object);
public static <T extends Throwable> T expectThrows(Class<T> expectedType, Executable executable);
public static void fail(String message);
}
public class SoftAssert {
public void assertEquals(Object actual, Object expected);
public void assertAll();
}Main TestNG class for programmatic test execution with extensive configuration options. Supports test class registration, suite configuration, listener management, and parallel execution control.
public class TestNG {
public TestNG();
public void setTestClasses(Class[] classes);
public void setXmlSuites(List<XmlSuite> suites);
public void addListener(Object listener);
public void setParallel(XmlSuite.ParallelMode parallel);
public void setThreadCount(int threadCount);
public void run();
public static void main(String[] argv);
}Comprehensive event system for customizing test behavior through listener interfaces. Provides hooks for test execution events, configuration methods, and suite lifecycle events.
public interface ITestListener {
void onTestStart(ITestResult result);
void onTestSuccess(ITestResult result);
void onTestFailure(ITestResult result);
void onTestSkipped(ITestResult result);
}
public interface ISuiteListener {
void onStart(ISuite suite);
void onFinish(ISuite suite);
}
public interface ITestResult {
int getStatus();
ITestNGMethod getMethod();
Object[] getParameters();
Throwable getThrowable();
long getStartMillis();
long getEndMillis();
}XML-based configuration system for defining test suites, tests, classes, and execution parameters. Enables complex test orchestration and parameterization without code changes.
public class XmlSuite {
public enum ParallelMode { TESTS, METHODS, CLASSES, INSTANCES, NONE }
public enum FailurePolicy { SKIP, CONTINUE }
public void setName(String name);
public void setParallel(ParallelMode parallel);
public void setThreadCount(int threadCount);
public void setTests(List<XmlTest> tests);
}
public class XmlTest {
public void setName(String name);
public void setXmlClasses(List<XmlClass> classes);
public void addParameter(String name, String value);
}Utility classes for collections, string manipulation, and reflection operations. Includes factory methods for creating lists, maps, and sets, plus helper utilities for common operations.
public class Lists {
public static <T> List<T> newArrayList();
public static <T> List<T> newLinkedList();
public static <T> List<T> intersection(List<T> list1, List<T> list2);
}
public class Maps {
public static <K, V> Map<K, V> newHashMap();
public static <K, V> Map<K, V> newConcurrentMap();
public static <K, V> ListMultiMap<K, V> newListMultiMap();
}
public class Reporter {
public static void log(String message);
public static ITestResult getCurrentTestResult();
public static List<String> getOutput(ITestResult result);
}// Test result constants
public interface ITestResult {
int SUCCESS = 1;
int FAILURE = 2;
int SKIP = 3;
int SUCCESS_PERCENTAGE_FAILURE = 4;
int STARTED = 16;
int CREATED = -1;
}
// Common interfaces
public interface ITestContext {
String getName();
Date getStartDate();
Date getEndDate();
IResultMap getPassedTests();
IResultMap getFailedTests();
IResultMap getSkippedTests();
}
public interface ITestNGMethod {
String getMethodName();
ITestClass getTestClass();
String[] getGroups();
String[] getDependsOnGroups();
String[] getDependsOnMethods();
boolean isTest();
IRetryAnalyzer getRetryAnalyzer();
}
// Exception types
public class TestNGException extends RuntimeException {
public TestNGException(String message);
public TestNGException(String message, Throwable cause);
}