13
Sep

Pass parameters to testng.xml via external source
==================================================

The easiest way to pass parameters to TestNG tests, is use <parameter> values in testng.xml.

<parameter name=”first” value=”dataone-first”></parameter>
<parameter name=”second” value=”dataone-second”></parameter

Above parameters including entire testng.xml can be injected with custom xmls at run time. Following steps elaborates how to do this

Step 1: Create your @Test with parameters
============================================

@Test
@Parameters(username)
public void testMeth(String name){
System.out.println(name);
}

Step 2: Create your custom xmls which contains above parameters and their values (under resources folder)
===========================================================================================================

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

Step 3: Create your testng.xml with <!ENTITY based injection as follow:
========================================================================

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” [<!ENTITY data SYSTEM “sharingParamsViatestng.xml”>]>
<suite name=”MyTestSuite” parallel=”false” verbose=”2″>
&data;
<test name=”MyTest”>
<classes>
<class name=”sharingParamsAcrossTestngSuites.ActualTest” />
</classes>
</test>
</suite>

Step 4: Execute your testng.xml with following VM Arguments
=============================================================
-Dtestng.dtd.http=true

Output:

[RemoteTestNG] detected TestNG version 7.0.0

… TestNG 7.0.0 by Cédric Beust (cedric@beust.com)

C:\Users\srasz\Desktop\prj4\JavaExamples\testng.xml
PASSED: f(“testng”)

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

Similarly other elements in testng.xml can be injected with multiple <!ENTITY injections as follow:

<?xml version = “1.0” encoding = “UTF-8”?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd” [
<!ENTITY listeners SYSTEM “listeners.xml”>
<!ENTITY classes SYSTEM “classes.xml”>
]>
<suite name=”Local Execution” verbose=”5″>
&listeners;
<test name=”Core Integration Tests” time-out=”800000″>
<groups>
<run>
<include name=”failed”/>
</run>
</groups>
&classes;
</test>
</suite>