Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 12, 2016

Understanding testng.xml file





Understanding testng.xml file will discuss in detail about the xml configuration file. testng.xml file is a configuration file in TestNG. It is used to define test suites and tests. It is also used to pass Parameters to the test methods. It provides different options to include packages, classes and independent test methods in our test suite. It also allows us to configure multiple tests in a single test suite and run them in multithreaded environment.

TestNG allows you to:

  • Create tests with packages
  • ‹‹ Create tests using classes
  • ‹‹ Create tests using test methods
  • ‹‹ Include/exclude a particular package, class, or test method
  • ‹‹ Use of regular expression while using the include/exclude feature
  • ‹‹ Store parameter values for passing to test methods at runtime
  • Configure multithreaded execution options

Now we will see how the XML file should look like by taking one sample example.

Prerequisites for creating an XML file:

  1. Eclipse should be installed in the machine.
  2. TestNG should be downloaded in the Eclipse.
  3. One sample Java project should be created in the Eclipse.

1. Sample Java Project

In the above screen, “TestProject” is sample Java program.

Now right click on the “TestProject”, then you can find the options called “TestNG → Convert to TestNG”. You can see the same in below screen

2. Context Menu

Click on “Convert to TestNG” option. Then it will prompt to the below screen.

3. Generate testng xml file

Here you can see some options called:

-Where to store the xml file (default will be in the Java Project folder)
-Suite Name
-Test Name
-Class Selection
-Parallel mode
-Preview of the XML file.
By default file will contain “Suite” and “Test” tags and these are the mandatory tags.





The name of the suite is mentioned using the name attribute (in this case Suite).It contains a test, declared using the XML tag test and the name of the test is given using the name attribute(in this case Test).

And you can include the Class Section in the XML file. Here you have two options to include first is “Classes” and second is “Packages”.

Now to discuss about the Classes and Packages will include one package and a class in the previously created java project. And the screenshot looks like below.

3.1 Project with Package and Class

In the above, “com.test.testngframework” is package and “SampleTest.java” is a class.

Now, while creating the testng.xml file we can include either packages or classes to execute using the configured XML file.

The below screen will show how to select the classes to include in the xml file to execute. For this you need to select the Classes option in the Class Selection drop down.

4. testng Classes Section

Then the XML file looks like this:

4.1 testng with classes

If you include classes in the xml file the class name comes along with the package name also. If you miss to give the package name the xml file will throw an error.





And the below screen will show how to select the packages to include in the xml file to execute. For this you need to select the Packages option in the Class Selection drop down.

Then the XML file looks like this:

4.2 testng with packages

If you include packages in the xml file then it will execute all the classes which are there under the same package.

We can also instruct the xml file to run the scripts parallelly. For this we need to select any one of the options from the Parallel mode dropdown. Here we have below options to select.

-Methods
-Classes
-Tests
By selecting any one of the option we will instruct the xml file to execute those things in parallel.

The screenshot for the same will be seen below:

5. testng Parallel Mode

For example, if you select “tests” as parallel mode option, then the xml file look like below:

5.1 Parallel with tests

This part will only discuss about the basics of the testng.xml file. Apart from this, we can modify or include so many configurations according to our needs. Will discuss all those things in the coming blogs in detail.



Share this post: on Twitter on Facebook

Selenium NoSuchWindowException Running testng.xml using Command Prompt

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025