13
Sep

Parameter sharing from parent testng.xml

Suite of suites in testng.xml

Working with suite of suites in TestNG
=======================================

Parameters can be shared between testng.xmls with suite of suites ie. parameters defined in Parent suite can be consumed by child suites.

Following steps demonstrates how to achieve this in TestNG.

Step 1: Create you @Tests
===========================

package sharingParamsAcrossTestngSuites;

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

public class ActualTest {
@Test
@Parameters({ “username” })
public void f(String userName) {
Assert.assertTrue(userName.equals(“testng”));
System.out.println(System.getProperty(“user.dir”)+”\\testng.xml”);
}
}

Step 2: Create a testng.xml suite for above test (eg. child-suite.xml)
========================================================================

<?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=”sharingParamsAcrossTestngSuites.ActualTest” />
</classes>
</test>
</suite>

Step 3: Create Parent suite (Suite of Suites) which defines parameters consumed by child-site.xml as follow:
=============================================================================================================

<?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/child-suite.xml”></suite-file>
</suite-files>
</suite>

Step 4: Execute parent suite
==============================

Output:
===============================================
MyTest
Tests run: 1, Failures: 0, Skips: 0
===============================================