TestNG - A Comprehensive Guide
1. Introduction to TestNG
TestNG (Test Next Generation) is an advanced Java
testing framework inspired by JUnit and NUnit but with more powerful
functionalities. It is widely used for unit testing, functional testing,
integration testing, and end-to-end testing.
Why Use TestNG?
- More
powerful annotations than JUnit.
- Parallel
execution of tests.
- Flexible
test configurations with XML.
- Data-driven
testing with @DataProvider.
- Reporting
and logging features.
- Supports
dependent tests, which are not available in JUnit.
2. Installation and Setup
Step 1: Add TestNG to Your Project
For Maven Projects (Add dependency in pom.xml):
xml
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
For Eclipse IDE Users
- Go
to Help > Eclipse Marketplace.
- Search
for TestNG and install it.
- Restart
Eclipse.
3. TestNG Annotations
Annotations in TestNG help define test cases clearly.
Annotation |
Description |
@Test |
Marks a method as a test case. |
@BeforeSuite |
Runs before the entire test suite. |
@AfterSuite |
Runs after the entire test suite. |
@BeforeTest |
Runs before any test methods in a <test> tag in XML. |
@AfterTest |
Runs after all test methods in a <test> tag in XML. |
@BeforeClass |
Runs before the first method of the current class. |
@AfterClass |
Runs after all methods in the current class. |
@BeforeMethod |
Runs before each test method. |
@AfterMethod |
Runs after each test method. |
@DataProvider |
Provides data for data-driven testing. |
@Parameters |
Used for parameterized testing via XML. |
4. Writing a Basic TestNG Test
java
import
org.testng.annotations.Test;
public class
TestExample {
@Test
public void testMethod() {
System.out.println("This is a
TestNG test case.");
}
}
Running the Test
- In
Eclipse: Right-click on the class > Run As > TestNG Test.
- In
IntelliJ IDEA: Right-click on the class > Run 'TestExample'.
5. Advanced TestNG Features
5.1 Grouping Tests
Use groups to categorize and run specific tests.
java
import
org.testng.annotations.Test;
public class
GroupExample {
@Test(groups = "smoke")
public void smokeTest() {
System.out.println("Smoke test
executed.");
}
@Test(groups = {"regression",
"sanity"})
public void regressionTest() {
System.out.println("Regression
& sanity test executed.");
}
}
Running specific groups from XML:
xml
CopyEdit
<suite
name="TestSuite">
<test name="GroupTest">
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
<classes>
<class name="GroupExample"/>
</classes>
</test>
</suite>
5.2 Data-Driven Testing with @DataProvider
Allows passing multiple sets of data to a single test.
java
import
org.testng.annotations.DataProvider;
import
org.testng.annotations.Test;
public class
DataProviderExample {
@DataProvider(name = "loginData")
public Object[][] dataProviderMethod() {
return new Object[][] {{"user1",
"pass1"}, {"user2", "pass2"}};
}
@Test(dataProvider = "loginData")
public void loginTest(String username,
String password) {
System.out.println("Login with:
" + username + " and " + password);
}
}
5.3 Parallel Execution
TestNG can execute tests in parallel to speed up
testing.
Parallel execution in testng.xml:
xml
<suite
name="ParallelTestSuite" parallel="methods" thread-count="3">
<test name="ParallelTests">
<classes>
<class name="TestClass1"/>
<class name="TestClass2"/>
</classes>
</test>
</suite>
5.4 Dependency Testing
Allows one test to depend on another test.
java
import
org.testng.annotations.Test;
public class
DependencyExample {
@Test
public void startApp() {
System.out.println("Application
started.");
}
@Test(dependsOnMethods =
"startApp")
public void login() {
System.out.println("User logged
in.");
}
}