How to Create TestNG.xml File in Eclipse

Faisal Khatri

Posted On: June 21, 2024

view count252609 Views

Read time14 Min Read

TestNG is a framework used for Java automation testing that makes creating and running automated tests much easier. One of its standout features is running tests in parallel using a TestNG.xml file, which expedites software release cycles.

This tutorial on how to create a TestNG.xml file in Eclipse dives deep into how to execute multiple test files from a single file known as the TestNG.xml file. Also, it explores creating a TestNG.xml file in Eclipse.

What Is the TestNG.xml File?

TestNG.xml is a configuration file that allows for organizing and executing the automated tests. It helps customize the test execution, including test suites, methods, parameters, and groups.

TestNG allows you to create multiple test cases simultaneously, and that is where the TestNG.xml file eases this task of writing various test cases all within single or multiple classes using TestNG Annotations.

Why Use TestNG.xml File?

The TestNG.xml file allows running tests in parallel, which could help the software teams save time and get faster feedback on the builds.

Here are the reasons to use the TestNG.xml file:

Prioritizes the test methods

TestNG.xml can be used to prioritize the test methods inside the test block. The TestNG.xml file allows for the specification of the tests in the order in which they need to be executed. This helps organize and efficiently run the test in the required order.

Allows parameterization in the tests

XML file allows parameterization, using which values used in the tests using the @Parameters annotation can be provided using the <parameter> tag in the TestNG.xml file.

A classic example for this case would be cross browser testing, where the browser placeholder is updated in the tests using @Parameters annotation, and multiple test blocks can be created in the TestNG.xml file.

To learn more about it, you can go through this blog on TestNG parameterization.

Provides parallel test execution

Another advantage of the TestNG.xml file is parallel testing. Using the parallel tag, test blocks under the test suite can be executed in parallel.

It can also help in parallel execution of the test methods provided inside each test block.

Provides TestNG listeners

The TestNG listeners can be added to the TestNG.xml file, thus helping to get the minute details about the test execution. This can help automation test engineers easily debug failed tests.

Offers group test execution

The TestNG.xml file allows group execution of the test cases. It will enable the executing the test method in a specific order as updated in the file. Therefore, while performing TestNG testing, the TestNG.xml file allows testers to control the test execution flow.

Info Note

Run Your Selenium Tests With TestNG on Cloud. Try LambdaTest Today!

Different Tags in a TestNG.xml File

Following is an example of the TestNG.xml file with multiple tests within a test suite and a single test with various tags inside it:

Let’s discuss the different tags used in the above TestNG.xml file:

<suite>

different tags used in the above TestNG.xml file

A TestNG.xml file has a single suite block. It is represented using the <suite> tag in the file. A suite can contain multiple test blocks within it.

<test>

TestNG.xml file has a single suite block

There can be multiple test blocks inside the TestNG.xml file. It is represented by the <test> tag and can contain one or multiple <classes> tags inside it.

<classes>

test blocks inside the TestNG.xml file

The <classes> tag has the name of the Java class that has the test methods written inside it. It can contain single or multiple test classes inside the <classes> tag block.

<methods>

Java class that has the test methods written inside

The <methods> tag can contain single or multiple test methods. These are the names of the actual test methods which will be executed for testing. The name of the test methods can be included using the <include> tag or excluded using the <exclude> tag. The <include> tag will queue up the test method for execution, while the < exclude > tag will skip the test, and the test will not be executed.

You can also enhance your Selenium test execution process by using AI-augmented testing agents such as KaneAI.

KaneAI by LambdaTest is an AI test assistant, featuring industry-first AI capabilities for test authoring, management, and debugging, tailored specifically for high-speed quality engineering teams. With KaneAI, users can effortlessly create and refine complex test cases using natural language, drastically cutting down the time and expertise needed to dive into test automation.

Setting Up a TestNG Project in Eclipse

Before creating a TestNG.xml file, you need to set up the TestNG project in Eclipse. If you don’t have Eclipse installed, refer to this guide to install TestNG in Eclipse.

Shown below are the steps:

  1. Create a new Java Project in Eclipse IDE using File > New > Java Project.
  2. Create a new Java Project in Eclipse IDE

  3. Enter the project name and click the Next> button.
  4. Enter the project name and click

  5. Navigate to the Libraries tab.
  6. Navigate to the Libraries tab

  7. Click the Add Library button.
  8. Click the Add Library button

  9. Select the TestNG library from the library options and click the Next> button.
  10. Select the TestNG library from the library options

  11. Click the Finish button to complete the library addition process.
  12. Finish button to complete the library addition process

  13. Click the Finish button to complete the project setup.
  14.  Finish button to complete the project setup

Create a New TestNG Class

Let’s now create a new TestNG class. This test will be executed using TestNG.

  1. Right-click on the project name > TestNG > Create TestNG class.
  2. Right-click on the project name

  3. Specify the additional information in the New TestNG class window, as shown in the screenshot below, and click the Finish button.
  4. New TestNG class window

  5. The value for the source folder should be the /src folder path in the project. Any name can be provided in the Package name field. The Class name field should be provided with the class name in which the tests will be written.
  6. Select the annotation as required. We have selected the @BeforeMethod and @AfterMethod annotations, which we will use in the test.

Write a Test

A new test class will be added as per the details provided in Step 2. You may notice the red marks in the test class. Hover the mouse over the error line, for example, on @Test annotation. Once the annotation is added, the errors will disappear.

new test class will be added as per the details

  1. Make the necessary changes in the class, like updating the test method name and writing the code in the respective @Test, @Before, and @After annotations.
  2. To run the tests, right-click on the class name > Run As > TestNG Test.
  3. run the tests, right-click on the class name

The tests will be executed using TestNG, and the results will be shown in the console window at the bottom.

 tests will be executed using TestNG

Subscribe to the LambdaTest YouTube Channel and stay updated with the latest video tutorials on Selenium Java.

Demo: How to Create a TestNG.xml File in Eclipse?

In the above section, we set up a TestNG.xml file and executed tests using TestNG. Now, let’s look at how to create a TestNG.xml file in Eclipse.

There are two methods to create a TestNG.xml file:

Method 1:

  1. Right-click on the project folder, and towards the bottom of the list, select TestNG and then Convert to TestNG.
  2. Right-click on the project folder

  3. A screen like the one below would appear. Click on Next.
  4. screen like the one below would appear

  5. The next window that pops up will have the refactored source code after you click Finish.
  6. next window that pops up will have the refactored source code

  7. A new addition to your project directory will be displayed, named as testng.xml.
  8. addition to your project directory will be displayed

    • Double-click on testng.xml to open the XML file, which looks like the one below.
    • Double-click on testng.xml to open the XML file

Method 2:

  1. Right-click on New, and select File.
  2. Right-click on New, and select File

  3. Enter the following file name testng.xml, and then click on Finish.
  4. Enter the following file name testng.xml

This is what a basic TestNG.xml file looks like.

what a basic TestNG.xml file

You have your very first TestNG.xml file ready. Now you know how to create a TestNG.xml file in Eclipse. Let’s leverage this TestNG.xml file to run automated tests using automation testing tools like Selenium. If you want to dig further and explore the true capabilities of Selenium, check out this guide on what is Selenium.

Running a TestNG.xml File

The hierarchy of an XML file goes like this- first <suite> tag, second <test> tag, and lastly, <classes> tag. You can give any name to the <suite> and <test> tags, but you have to be careful while naming classes since it combines the package and test name.

Since the suite is the topmost hierarchy, this is where multiple test classes can be placed. Let’s understand with the help of an example. For the test scenarios below, we have created two Java classes wherein both tests will run on the LambdaTest eCommerce Playground website.

Test Scenario 1:

  1. Check the title of the LambdaTest eCommerce Playground website.

Test Scenario 2:

  1. Start by searching for the product iphone from the home page.
  2. Verify that the search results page correctly displays the title Search – iphone to ensure you are on the right page.

Implementation:

The following code will help implement test scenarios 1 and 2. The EcommercePlayGroundTests class has both test methods to implement the test scenarios.

There are two additional methods in the class. The first one is the setup() method, which will run before any test runs and help set up the WebDriver and start the Chrome browser. The second tearDown() method will gracefully close the WebDriver session.

In case you want to know more about WebDriver, you can go through this tutorial on what is Selenium WebDriver.

github

The testCheckTitle() method will implement the Test Scenario 1, whereas the testSearchForProduct() method will implement the Test Scenario 2.

Test Execution:

Let’s create the TestNG.xml file with the name testng-demo.xml and update both test method names to execute the tests.

In the TestNG.xml file above, there are 2 test methods that have the <include> tag and belong to the EcommercePlaygroundTests class. When this file is executed, both tests will run in parallel in the order in which they are updated.

First, the testCheckTitle() method will run, next the testSearchForProduct() method will be run. To run the tests methods in parallel, we need to update the parallel = “methods” tag in the <test> tagline.

To run the project using the XML file, right-click on the testng.xml file and select Run As > TestNG Suite.

 project using the XML file, right-click on the testng.xml

This will execute and run both of the tests using testng.xml with a simple click. Following is the test execution result displayed in the console.

tests using testng.xml with a simple click.

Parallel Testing Using TestNG.xml File

Now that you have executed your TestNG.xml file successfully. As mentioned earlier, this file helps running tests in parallel. So, it is important to note the changes you need to make in the TestNG.xml to perform automation testing. However, configuring hundreds of browsers and operating systems in your nodes doesn’t look feasible enough.

Instead, you can leverage a cloud grid offered by LambdaTest. It is an AI-powered test orchestration and execution platform that lets developers and testers perform Selenium automation testing with TestNG on over 3000+ real browsers and operating systems online. That way, you only need to focus on writing better Selenium automation scripts without worrying about test infrastructure needs.

Let’s use the same test scenario we implemented in the earlier section of the blog, where we searched for a product on the LambdaTest eCommerce Playground website. Earlier, we ran the tests on the local Chrome browser.

Test Scenario:

  1. Start by searching for the product iphone from the home page.
  2. Verify that the search results page correctly displays the title Search – iphone to ensure you are on the right page.

Implementation:

A new test class, Base.java, has been created to take care of all the configurations related to running tests on the LambdaTest cloud grid. It will also take care of starting and closing the browsers.

The values for the browser name, version, and platform name will be provided on runtime using the testng.xml file.

The setup() method will update the browser, its version, and platform name as the values are set using the @Parameters annotation in TestNG.

The capabilities required for configuration can be set using the LambdaTest Automation Capabilities Generator.

Code Walkthrough:

The getLtOptions() method will set the required capabilities in the test scripts.

Next, a new test class ECommercePlaygroundTestsOnCloud, has been created to implement the test scenario and extend the Base class.

The testSearchForProduct() method will search for the product iphone from the website’s home page. After loading the search page, it will check that the page title equals Search- iphone.

If everything works fine, the status variable will be updated to passed.

Test Execution:

Let’s create a new TestNG.xml file and name it as testng-parallel.xml. That will help to perform Selenium testing on two different browsers and platforms in parallel.

The parallel = “tests” will execute both tests in parallel.

The details of the test execution can be found on the LambdaTest Web Automation Dashboard.

 test execution can be found on the LambdaTest Web Automation

Conclusion

In this blog on how to create a TestNG.xml file in Eclipse, we discussed the importance of using the TestNG.xml file.

With the TestNG.xml file, testers can organize the tests, customize and run the tests in parallel, and group the tests. It also helps in the parameterization of the tests and provides more visibility towards the test execution.

Frequently Asked Questions (FAQs)

How do I run the tests using the TestNG.xml file?

Create a TestNG.xml file and place it in the root folder of the project. Select the file and right click on it, and select the option “Run ../testng.xml’ to execute the tests.

Can we create multiple TestNG.xml files within a project?

Yes, multiple TestNG.xml files can be created within a project to organize and execute the tests.

Which tag can be used in the TestNG.xml file for parallel execution of the tests?

Within the <suite> tag, parallel=“tests” can be used to execute the tests in parallel.

How to create a TestNG project in Eclipse?

To create a TestNG project in Eclipse, first, ensure TestNG is installed. Then, go to “File,” “New,” “Project,” select “TestNG” under the “Java” category, and follow the prompts to create the project.

Author Profile Author Profile Author Profile

Author’s Profile

Faisal Khatri

Faisal is a Software Testing Professional having 14+ years of experience in automation as well as manual testing. He is a QA, freelancer, blogger and open source contributor. He loves learning new tools and technologies and sharing his experience by writing blogs.

Blogs: 39



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free