//
you're reading...
TestNG

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 {
    @Test
    @Parameters({ "username" })
    public void f(String userName) {
        Assert.assertTrue(userName.equals("testng"));
    }
}

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">
		<classes>
			<class name="rationale.emotions.ReadParameters" />
		</classes>
	</test> 
</suite>  

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-files>
		<suite-file path="src/test/resources/testng-childsuite.xml"></suite-file>
	</suite-files>
</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">
	<parameters>&parent;</parameters>
	<test name="MyTest">
		<classes>
			<class name="rationale.emotions.ReadParameters" />
		</classes>
	</test> 
</suite> 

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.

Advertisements

Discussion

3 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)

    &parent;

    And the test does this:

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

    @BeforeClass
    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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: