you're reading...

Sharing parameters among different TestNG suites.

Off late I have been seeing a couple of queries every now and then which asks “How do I share parameters across multiple TestNG suite xmls”, so I thought I would perhaps write up a post which details it.

There are two ways in which this can be done.

Approach (1)

Leverage what TestNG directly has to offer, which is “create a suite of suites”. Incase you are wondering how to get this done, here’s how you do it.

For example sake, lets assume that we have the below test class.

package rationale.emotions;

import org.testng.Assert;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ReadParameters {
    @Parameters({ "username" })
    public void f(String userName) {

As you can see this is a simple Test class, which accepts a parameter via the @Parameters annotation and then runs an Assert on it.

Now lets look at how our suite xml file would look like : [ We will call this suite file as "testng-childsuite.xml" which is located under src/test/resources in my project]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MyTestSuite" parallel="false" verbose="2">
	<test name="MyTest">
			<class name="rationale.emotions.ReadParameters" />

Now lets create a Suite of Suites as below : [ We will call this suite file as “testng-mastersuite.xml”]

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MasterSuite" parallel="false">
	<parameter name="username" value="testng"></parameter>
		<suite-file path="src/test/resources/testng-childsuite.xml"></suite-file>
</suite> <!-- MasterSuite -->

Now run “testng-mastersuite.xml” and you will see that the parameter “username” that I had in my master suite automatically got passed over to the child suite viz., testng-childsuite.xml automatically and the assert passes.

Approach (2)

This was a new learning for me as well and I picked up this from this post on the testng-users google forum.

Our Test class is going to remain the same. So I am not going to be duplicating it again here.

Now lets create a simple xml file and add up all the parameters that we feel need to be shared across multiple suites.
I will call it my “parameters.xml” and it is available under src/test/resources folder in my project.

Here’s how it would look like :

<?xml version="1.0" encoding="UTF-8"?>
<parameter name="username" value="testng"></parameter>

Now lets create a basic TestNG xml suite file and then refer to the above mentioned parameters.xml in it.
Lets call it as “standalone-testsuite.xml”

Here’s how it would look like :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" [<!ENTITY parent SYSTEM "src/test/resources/parameters.xml">]>
<suite name="MyTestSuite" parallel="false" verbose="2">
	<test name="MyTest">
			<class name="rationale.emotions.ReadParameters" />

Couple of items that are worth mentioning here.

[<!ENTITY parent SYSTEM "src/test/resources/parameters.xml">]> is basically our way of saying that this xml file has an entity (which we will call as “parent”) and it maps to the resource parameters.xml that resides under src/test/resources folder.

&parent; is basically our way of saying that within the tag we are going to be replacing the contents of parameters.xml, which we are denoting via the name “parent” (notice that we gave our entity its name as “parent”). The entity name is supposed to be prefixed by an ampersand and it is supposed to end with a semi-colon.

Now run the standalone-testsuite.xml and you would notice that the assert passes.

Hope that helps you understand how to share parameters across different TestNG suite xmls.


6 thoughts on “Sharing parameters among different TestNG suites.

  1. Interesting one!

    Posted by Murali Lakshmanan | February 6, 2014, 1:44 pm
  2. Goodone!!pls keep adding new ones

    Posted by Chris Jr | August 15, 2014, 8:33 pm
  3. This is great, but the example didn’t work for me. I have to do this:

    parameter file

    In the child xml.
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd&quot; [] > (Note I put these 2 xml in the settings folder, which is not on the current project home, IDE will complain that it can’t resolve the file, just ignore it, it will find it at run time)


    And the test does this:

    private XmlTest config
    String p1 = “”,
    p2 = “”,
    t1 =””,
    t2 =”” ,
    p3 = “”

    public void setUp(XmlTest config) throws Exception

    this.config = config
    asBaseUITest.config = config
    p1 = config.getParameter(“ip_instance”);
    p2 = config.getParameter(“filename”);
    t1 = config.getParameter(“a_name”);
    t2 = config.getParameter(“b_name”)


    @Test(enabled = true)
    @Parameters([“ip_instance”, “filename”])
    void testXML(String param1, String param2)
    println “value of p1 from parent xml: $p1”
    println “value of p2 from parent xml: $p2”
    println “value of p3 from parent xml: $p3”
    println “value of t1 from child xml: $t1”
    println “value of t2 from child xml: $t2”
    println “value of param1: $param1”
    println “value of param2: $param2”


    Posted by BangkokKid | October 27, 2015, 11:09 pm
  4. Great article! This is exactly what I was looking for. I have an additional observation: If you want to override any of the common parameters defined in parameters.xml, then just mention its overridden value just below “&parent;” in the child testng.xml file.

    Posted by Akshay Maldhure | June 23, 2017, 1:42 pm
  5. Thank you for this article.
    However, when I run this test suite I got the following error: “[ERROR] org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 14; Element type “parameters” must be declared.”

    Any idea on what could be the problem ? My testng version is 6.14.3.

    Posted by Jean | June 15, 2018, 6:36 pm
  6. Followed articles, for
    &parent; getting this elements not allowed.

    Posted by Vipindas | December 17, 2018, 5:35 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: