Multiple Tests with TestNG.XML

TestNG provides an option to execute multiple tests in a single configuration file (testng.xml). It allows to divide tests into different parts and group them in a single tests. We can group all the tests related to database into one group, Regression tests in one group. And all the test cases related to Unit test cases into one group and so on..

TestNG makes it easy by following the below Syntax to add multiple tests in testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Suite" verbose="1" >
  <test name="Unit Level Test Cases" >
    <classes>
       -- Specify Class name here ---
    </classes>
  </test>

  <test name="Regression Test Cases">
    <classes>
      -- Specify Class name here ---
    </classes>
  </test>
</suite>

Let us create an example to execute multiple tests with the help of TestNG.xml

First we will create two different packages names “com.easy.entry” and “com.easy.record”. And then create classes under each package.

Let us create two classes under package “com.easy.entry” and add classes.

Class 1:

package com.easy.entry;
import org.testng.annotations.Test;

public class AddTestCase {
	@Test
	public void addLocationTestCase() {
		System.out.println("Im in add location test case");
	}
}

Class 2:

package com.easy.entry;
import org.testng.annotations.Test;

public class EditTestCase {
	@Test
	public void editLocationTestCase() {
		System.out.println("Im in edit location test case");
	}

In the same way now lets us create classes under package “com.easy.record”.

Class 1:

package com.easy.record;
import org.testng.annotations.Test;

public class AddUserTestCase {
	@Test
	public void addUserWithValidData() {
		System.out.println("Adding user with valid data");
	}
}

Class 2:

package com.easy.record;
import org.testng.annotations.Test;

public class DeleteUserTestCase {
	@Test
	public void deleteUserData() {
		System.out.println("Deleting the user");
	}
}

We have created simple classes and now we will define testng.xml to execute the above tests.

We have given suite name as “Sample Multiple Test Suite” . We have also named the two tests as “Unit Level Test” and

“Regression Test” in the below example and adding the classes between tests.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Sample Suite" verbose="1" >
  <test name="Unit Level Test" >
    <classes>
       <class name=”com.easy.entry.AddTestCase" />
<class name=”com.easy.entry.EditTestCase" />
    </classes>
  </test>
  <test name="Regression Test">
    <classes>
      <class name=”com.easy.records.AddUserTestCase" />
<class name=”com.easy.records.DeleteUserTestCase" />
    </classes>
  </test>
</suite>

Now after adding the above to testng.xml file and when we execute XML file as a TestNG suite, it will be executed each class separately. And Now the output should look like below.

testng multiple tests

And also when you check with the testng report html file, it will also show the number of tests that are executed

testng multiple tests report

Test Frameworks: 

Comments

Hi, Thanks for the tutorial.
Can you please tell em if I want to run, say, "com.easy.records.AddUserTestCase" twice, how I wil be able to do that?
TIA.

Hi,
Thank you for the tutorial, I am getting error while running testng.xml with multiple test classes first one will executes 2nd one will give null point Exception like doing login and updating some records,login will be executed and all other tests won't run.
Is there any solution for this .
Thanks

I am getting error while running testng.xml with multiple test classes first one will executes 2nd one will give null point Exception like doing login and updating some records,login will be executed and all other tests won't run.
Is there any solution for this .

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.