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

public void testMeth(String 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″>
<test name=”MyTest”>
<class name=”sharingParamsAcrossTestngSuites.ActualTest” />

Step 4: Execute your testng.xml with following VM Arguments


[RemoteTestNG] detected TestNG version 7.0.0

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

PASSED: f(“testng”)

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″>
<test name=”Core Integration Tests” time-out=”800000″>
<include name=”failed”/>