1

We are trying to add a system property directly in the testng xml file, as we want to automatically run different test suites in a Jenkins job, without having different xml files for each test set. Is there a way to pass a system property directly to the file and parse it?

For example, we currently have:

<test name="Regression">
        <groups>
            <run>
                <include name="Test" />
            </run>
        </groups>

and would like to be able to have:

<test name="Regression">
    <groups>
        <run>
            <include name=$TestParameter />
        </run>
    </groups>

Thanks a lot!

pboix
  • 23
  • 1
  • 3

1 Answers1

1

In TestNG there is no interpreter that can translate your 'variable' into the value from system properties. You have to add that functionality yourself. Take a look at http://testng.org/doc/documentation-main.html#running-testng-programmatically. Here's an example that fits your suggestion:

XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");

XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
List<XmlClass> classes = new ArrayList<XmlClass>();
classes.add(new XmlClass(System.getProperty("TestParameter")));
test.setXmlClasses(classes) ;

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run(); 

There are of course other ways to do this. A simpler way may be to run a pre-test script that simply replaces content in an XML file before the tests start. Do it using a groovy script for example:

def file = new File('relativepath/file')
def xml = file.text
//println xml
file.text = xml.replace('$TestParameter',System.getProperty('TestParameter'))

Remember NOT to use " when defining $TestParameter since groovy will interpret that as an attempt to use a local variable with that name.

Jocce Nilsson
  • 1,658
  • 14
  • 28