Creating Test Suite with TestNG
What is a Test Suite?
A Test Suite groups multiple test classes together, configured through testng.xml, and run as a single execution.
Example
<suite name="Regression Suite">
<test name="Login Tests">
<classes>
<class name="tests.LoginTest"/>
<class name="tests.LogoutTest"/>
</classes>
</test>
</suite>
Why This Matters
Suites let a team run a full regression pack, or a smaller smoke-test subset, with a single command — useful both locally and in a CI/CD pipeline like Jenkins.
Building a Fuller testng.xml
A suite file can do much more than list classes. It can share parameters, filter by groups, pick individual methods and run tests in parallel.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression Suite" parallel="tests" thread-count="2">
<parameter name="baseUrl" value="https://staging.example.com"/>
<test name="Chrome Smoke">
<parameter name="browser" value="chrome"/>
<groups>
<run>
<include name="smoke"/>
<exclude name="flaky"/>
</run>
</groups>
<packages>
<package name="tests.*"/>
</packages>
</test>
<test name="Firefox Checkout">
<parameter name="browser" value="firefox"/>
<classes>
<class name="tests.CheckoutTest">
<methods>
<include name="placeOrderWithCard"/>
</methods>
</class>
</classes>
</test>
</suite>
Each <test> block is a separate run unit. With parallel="tests", the Chrome and Firefox blocks run at the same time on two threads.
Connecting Tests to the Suite
Test methods read suite parameters with @Parameters and are tagged with groups so the suite can filter them.
public class LoginTest {
@Parameters({"browser", "baseUrl"})
@BeforeClass
public void setUp(String browser, String baseUrl) {
driver = DriverFactory.create(browser);
driver.get(baseUrl + "/login");
}
@Test(groups = {"smoke"})
public void validLoginShowsDashboard() { /* ... */ }
@Test(groups = {"regression"})
public void lockedUserSeesError() { /* ... */ }
}
Running the Suite
- From the IDE: right-click
testng.xmland choose run as TestNG suite. - With Maven: point the Surefire plugin at the file using
suiteXmlFiles, then runmvn test. Teams often keepsmoke.xmlandregression.xmland pick one with a Maven property. - In CI: the pipeline runs the same Maven command and publishes the reports TestNG writes to
test-outputortarget/surefire-reports.
A useful habit: after a failed run, TestNG writes testng-failed.xml, which contains only the failed tests. Running it is a quick way to confirm a fix.
Common Mistakes
- Misspelling a fully qualified class name, which stops the run with a class-not-found error.
- Enabling parallel runs while tests share a static WebDriver, which causes random failures.
- Using groups in the suite but forgetting to tag methods, so zero tests run.
- Expecting
@BeforeClasssetup methods to run when their group is filtered out; addalwaysRun = trueto configuration methods.
Structuring Suites in a Growing Project
As the number of tests grows, one huge suite file becomes hard to manage. A cleaner approach is to keep small, focused suite files and combine them in a master file.
<suite name="All Tests">
<suite-files>
<suite-file path="smoke.xml"/>
<suite-file path="regression.xml"/>
<suite-file path="api.xml"/>
</suite-files>
</suite>
Teams often follow a few simple conventions. Keep a fast smoke suite that finishes in minutes and runs on every pull request. Keep a full regression suite for nightly builds. Use groups for cross-cutting labels such as "payments" or "critical", and keep package names consistent so tests.* patterns pick up new classes automatically. Finally, add listeners such as reporting or retry listeners in the suite file with a <listeners> block, so every run gets the same behaviour without changing test code. Review the suite files regularly, just as you review test code, because they decide what actually runs.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.