#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
TestNG & Maven

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

  1. suite: the root element. parallel can be methods, classes, tests or instances, and thread-count sets how many run at once.
  2. listeners: plug in classes that react to test events, such as taking a screenshot on failure.
  3. parameter: a value defined at suite level is visible everywhere; one inside a test block overrides it for that block only.
  4. groups: include or exclude tests tagged with @Test(groups = "...").
  5. packages: pick up every class in a package instead of listing each one.
  6. 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 name must 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 @Optional is given.
  • Flaky parallel runs: shared static drivers or data clash once parallel is 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.

Explore Course