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:
- Eclipse should be installed in the machine.
- TestNG should be downloaded in the Eclipse.
- One sample Java project should be created in the Eclipse.
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
Click on “Convert to TestNG” option. Then it will prompt to the below screen.
Here you can see some options called:
-Where to store the xml file (default will be in the Java Project folder)
-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.
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.
Then the XML file looks like this:
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:
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.
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:
For example, if you select “tests” as parallel mode option, then the xml file look like below:
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.