Create TestNG.xml file
Purpose
The testng.xml file configures which test classes and methods run, in what order, and with what settings — acting as the entry point for running a TestNG suite.
Basic Example
<suite name="API Suite">
<test name="Smoke Tests">
<classes>
<class name="com.example.tests.LoginApiTest"/>
</classes>
</test>
</suite>
What Else It Can Control
The XML file can define test groups, parallel execution settings, parameters passed into tests, and multiple <test> blocks representing different suites (e.g., Smoke, Regression).
A Fuller testng.xml You Can Adapt
Real projects usually need more than one class. The file below shows the most useful elements together:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression Suite" parallel="classes" thread-count="3">
<listeners>
<listener class-name="com.example.listeners.ScreenshotListener"/>
</listeners>
<parameter name="baseUrl" value="https://staging.example.com"/>
<test name="Smoke">
<groups>
<run>
<include name="smoke"/>
<exclude name="slow"/>
</run>
</groups>
<packages>
<package name="com.example.tests.*"/>
</packages>
</test>
<test name="Checkout Only">
<parameter name="browser" value="firefox"/>
<classes>
<class name="com.example.tests.CheckoutTest">
<methods>
<include name="payByCard"/>
</methods>
</class>
</classes>
</test>
</suite>
Reading the File Line by Line
- suite: the root element.
parallelcan be methods, classes, tests or instances, andthread-countsets how many run at once. - listeners: plug in classes that react to test events, such as taking a screenshot on failure.
- parameter: a value defined at suite level is visible everywhere; one inside a
testblock overrides it for that block only. - groups: include or exclude tests tagged with
@Test(groups = "..."). - packages: pick up every class in a package instead of listing each one.
- methods: narrow a class down to particular test methods.
Parameters reach Java code through the @Parameters annotation:
@Parameters({"baseUrl", "browser"})
@BeforeClass
public void setUp(String baseUrl, @Optional("chrome") String browser) {
driver = DriverFactory.create(browser);
driver.get(baseUrl);
}
@Optional supplies a default when the XML does not define that parameter, which keeps the class runnable from the IDE as well.
The DOCTYPE line at the top is worth keeping. It lets IDEs validate the file and offer autocomplete for element and attribute names, so a typo such as <clases> is caught while you type instead of producing a silent "no tests found" result. Order also matters: by default TestNG runs <test> blocks in the order they appear, which lets you place quick smoke checks before the longer functional blocks so a broken build fails fast.
Running the Suite from Maven
Point the Surefire plugin at the file so mvn test runs exactly the suite you defined:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Many teams keep separate files such as smoke.xml and regression.xml, and the CI pipeline chooses one depending on the stage. In an IDE, right-clicking the XML file and choosing Run also works.
Errors That Stop a Suite from Running
- Class not found: the
namemust be the fully qualified class name, including the package. - Zero tests run: a group name in the XML does not match the annotation exactly; names are case-sensitive.
- Parameter errors: a method asks for a parameter that the XML never defines and no
@Optionalis given. - Flaky parallel runs: shared static drivers or data clash once
parallelis switched on. - Duplicate test names: each
<test>block in a suite needs a unique name.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.