Blog Details Shape

How to Use TestNG.xml? Step-by-Step Guide for Beginners

Pratik Patel
By
Pratik Patel
  • Mar 22, 2025
  • Clock
    4 min read
How to Use TestNG.xml? Step-by-Step Guide for Beginners
Contents
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.

Ever felt stuck running Selenium test cases one by one, wishing there was an easier way? Good news—there is! Meet TestNG, the ultimate testing framework for organizing and automating your test execution effortlessly.

With TestNG.xml, you can run multiple test cases in one go, control execution flow, and even group tests based on priority. No more manually triggering tests—just set it up once and let it handle everything for you.

But what is TestNG, and how can it make your life easier? In this guide, we’ll break it all down—what TestNG.xml is, why it’s a game-changer, and how to set it up in Eclipse like a pro. Let’s dive in!

{{cta-image}}

What is TestNG.xml?

TestNG.xml is a configuration file used in the TestNG framework to define and organize your test suites. Think of it as a roadmap that tells TestNG which tests to run, in what order, and with what configurations. TestNG.xml allows you to define test parameters to control test execution settings.

For example, if you have 100 test cases but you want to run only 20 of them, TestNG.xml lets you do this easily. It is a powerful tool that makes test automation more manageable and efficient, especially when combined with TestNG xml selenium integration.

What are the Advantages of Using TestNG.xml?

If you’re still wondering why you should bother with TestNG.xml, here are some key advantages that make things clear:

What are the Advantages of Using TestNG.xml?
  • Organizes Test Suites: Instead of running tests randomly, TestNG.xml lets you group and organize them logically.
  • Parallel Test Execution: Want to run tests faster? TestNG.xml parallel execution allows you to execute tests simultaneously, saving you tons of time.
  • Supports Parameterization: With TestNG parameters, you can pass different inputs to your tests without changing the code.
  • Grouping Tests: Run specific groups of tests, like smoke tests and regression tests, with ease.

In short, TestNG.xml is a game-changer for anyone serious about test automation.

Basic Structure of TestNG.xml

A TestNG.xml file is an XML configuration file that defines how tests are executed in the TestNG framework. It follows a structured format using specific tags to organize test suites, test groups, classes, and methods.

Below is the basic structure of a TestNG.xml file:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite">
    <test name="LoginTests">
        <classes>
            <class name="tests.LoginTest"/>
        </classes>
    </test>
</suite>
Copied!

1. suite Tag

The suite tag is the root element and represents a collection of test cases or test groups. It allows you to organize and execute multiple tests together.

<suite name="MyTestSuite">
</suite>
Copied!

2. test Tag

The test tag defines a group of related test cases. It can include multiple test classes that belong to the same module or functionality.

<test name="LoginTests">
        <classes>
            <class name="tests.LoginTest"/>
        </classes>
    </test>
Copied!

3. classes Tag

The classes tag contains one or more class tags, where each <class> represents a Java class that includes TestNG test methods.

<classes>
            <class name="tests.LoginTest"/>
            <class name="tests.SignUpTest"/>
        </classes>
Copied!

4. methods Tag

The methods tag is used when you want to execute only specific test methods from a class rather than all test methods.

<class name="tests.LoginTest"/>
   <method>
            <include name="testValidLogin"/>
            <include name="testInvalidLogin"/>
        </method>
      </class>
Copied!

How to Create a TestNG.xml File in Eclipse?

Here's a step-by-step guide on how to create TestNG.xml file in Eclipse​ to manage your automated test suites effectively:

How to Create a TestNG.xml File in Eclipse?

Step 1: Set Up Your TestNG Project in Eclipse

Ensure you have TestNG installed in your Eclipse IDE:

  • Open Eclipse.
  • Click Help → Eclipse Marketplace.
  • Search for TestNG and install it if not already present.
  • Restart Eclipse to apply changes.

Step 2: Create a New TestNG XML File

  • Right-click your project in Eclipse.
  • Select New → File.
  • Name the file TestNG.xml and click Finish.

Step 3: Write the Basic Structure

Add the following basic structure to your newly created TestNG.xml file:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="ExampleSuite">
    <test name="ExampleTest">
        <classes>
            <class name="com.project.tests.ExampleTestClass"/>
        </classes>
    </test>
</suite>
Copied!

Step 4: Add Test Classes and Methods

Specify your desired test classes and methods:

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite1" parallel="tests" thread-count="2">
    <test name="LoginTests">
        <classes>
            <class name="com.project.tests.LoginTest">
                <methods>
                    <include name="testValidLogin"/>
                </methods>
            </class>
        </classes>
    </test>
    <test name="SignupTests">
        <classes>
            <class name="com.project.tests.SignupTest"/>
        </classes>
    </test>
</suite>
Copied!

To assign test methods to multiple test groups within the TestNG XML file, you can use the groups tag to categorize your tests based on their functionalities, enhancing testing efficiency and clarity.

Here, the parallel="tests" attribute enables parallel execution, significantly speeding up your automated tests.

Step 5: Save and Verify

  • Save the TestNG.xml file.
  • Ensure it's correctly placed in your project's root directory or specified folder.
  • Verify that all the test methods are correctly included in the TestNG XML file to ensure proper execution of your test cases.

How to Run XML File in TestNG

Once your TestNG.xml file is ready, how to run xml file in TestNG is straightforward:

  1. Right-click the TestNG.xml file in Eclipse.
  2. Select Run As → TestNG Suite.
  3. TestNG will execute the tests as defined in the XML file.

{{cta-image-second}}

Conclusion

And there you have it—a complete guide to using the TestNG.xml file! Whether you’re organizing test suites, running tests in parallel with TestNG.xml parallel, or integrating with TestNG xml selenium, the TestNG framework makes your life so much easier.

If you’re just starting out, I recommend playing around with the basic structure and gradually exploring advanced features like TestNG parameters and grouping. Trust me, once you get the hang of it, you’ll wonder how you ever managed without it.

Something you should read...

Frequently Asked Questions

How do I run TestNG.xml from the command line?
FAQ ArrowFAQ Minus Arrow

You can run TestNG.xml from the command line using this command:

 java -cp "path/to/testng.jar:path/to/your/project/classes" org.testng.TestNG testng.xml
Copied!
Can I use TestNG.xml with Selenium?
FAQ ArrowFAQ Minus Arrow

Absolutely! TestNG.xml works seamlessly with Selenium. Just add your Selenium test classes to the <classes> tag in the XML file.

What is the difference between TestNG and JUnit?
FAQ ArrowFAQ Minus Arrow

While both are testing frameworks, TestNG offers more advanced features like parallel execution, parameterization, and flexible test configuration through TestNG.xml.

What is XML file in Selenium?
FAQ ArrowFAQ Minus Arrow

In Selenium, test execution settings are configured and controlled using an XML file, same like in TestNG. Users can select test parameters, define test suites, define test classes and methods, and customize additional testing settings. To manage and run automated tests, Selenium frequently uses XML files in combination with testing frameworks such as TestNG or JUnit.

About the author

Pratik Patel

Pratik Patel

Pratik Patel is the founder and CEO of Alphabin, an AI-powered Software Testing company.

He has over 10 years of experience in building automation testing teams and leading complex projects, and has worked with startups and Fortune 500 companies to improve QA processes.

At Alphabin, Pratik leads a team that uses AI to revolutionize testing in various industries, including Healthcare, PropTech, E-commerce, Fintech, and Blockchain.

More about the author

Discover vulnerabilities in your  app with AlphaScanner 🔒

Try it free!Blog CTA Top ShapeBlog CTA Top Shape
Join 1,241 readers who are obsessed with testing.

Discover vulnerabilities in your app with AlphaScanner 🔒

Blog CTA Top ShapeBlog CTA Top ShapeTry it free!

Blog CTA Top ShapeBlog CTA Top Shape
Oops! Something went wrong while submitting the form.
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.
Join 1,241 readers who are obsessed with testing.
Consult the author or an expert on this topic.
Pro Tip Image

Pro-tip

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Related article:

Make the right choice and start your web browser testing today!Maximize your software's quality with expert qa tester!