Building Test Suites in TestNG
✅ What is a Test Suite in TestNG?
A TestNG test suite is a collection of tests that are executed together. It allows you to:
Run multiple test classes.
Set test execution order.
Pass parameters to tests.
Include/exclude groups of tests.
π ️ Steps to Build a Test Suite in TestNG
Step 1: Create Your Test Classes
java
Copy
Edit
// File: TestClass1.java
import org.testng.annotations.Test;
public class TestClass1 {
@Test
public void testMethod1() {
System.out.println("TestClass1 - testMethod1");
}
}
java
Copy
Edit
// File: TestClass2.java
import org.testng.annotations.Test;
public class TestClass2 {
@Test
public void testMethod2() {
System.out.println("TestClass2 - testMethod2");
}
}
Step 2: Create the testng.xml File
xml
Copy
Edit
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="MyTestSuite">
<test name="SampleTests">
<classes>
<class name="TestClass1"/>
<class name="TestClass2"/>
</classes>
</test>
</suite>
<suite>: Defines the suite.
<test>: Represents a group of test classes.
<classes> and <class>: List of classes containing test methods.
Step 3: Run the Test Suite
You can run the suite using:
Command Line (Maven):
sh
Copy
Edit
mvn test -DsuiteXmlFile=testng.xml
From IDE (like IntelliJ or Eclipse):
Right-click on the testng.xml file.
Click Run 'testng.xml'.
π§ Advanced Features in TestNG Suites
1. Grouping Tests
java
Copy
Edit
@Test(groups = "smoke")
public void testLogin() {}
@Test(groups = "regression")
public void testCheckout() {}
In testng.xml:
xml
Copy
Edit
<groups>
<run>
<include name="smoke"/>
</run>
</groups>
2. Parameterization
java
Copy
Edit
@Test
@Parameters("browser")
public void testLogin(String browser) {
System.out.println("Testing on browser: " + browser);
}
In testng.xml:
xml
Copy
Edit
<parameter name="browser" value="chrome"/>
3. Parallel Execution
xml
Copy
Edit
<suite name="ParallelSuite" parallel="classes" thread-count="2">
π Summary
Concept Purpose
testng.xml Defines the structure of the test suite
<suite> Groups tests into a suite
<test> Groups test classes
<classes> Lists the test classes
Groups Run subsets like smoke, regression
Parameters Pass values dynamically to tests
Parallel Execution Run tests faster using threads
Learn Testing Tools Training in Hyderabad
Read More
Introduction to Robot Framework
Setting Up Playwright for the First Time
How to Write BDD Tests in Cucumber
Mobile Automation with Appium 101
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment