Christoph Zabinski Christoph Zabinski - 11 months ago 105
Java Question

TestNG: how do I run a custom TestNG.XML File programmatically

I browsed through several different threads and websites (as well as the TestNG API) looking for how to run and create custom tests. I haven't found (or not understood) how I can run a custom testng.xml (test suite) programmatically.

I have created a testng.xml file looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="The Login Suite'" verbose="1" >
<test name="Some login check" >
<class name="tests.first_login"/>
<test name="Another login check" >
<class name="tests.second_login"/>

I know I can execute my suite from out of Eclipse - but I don't want that. Why? Because I am having the test run by a scheduler. So I need to have a configurable testng.xml file.

I have tried to generate a virtual XML Test suite by code looking like this:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();

XmlSuite loginSuite = new XmlSuite();
loginSuite.setName("The Login Suite");

XmlTest first_test = new XmlTest();
first_test.setName("Some login check");

List<XmlClass> fistlogin_classes = new ArrayList<XmlClass>();
fistlogin_classes.add(new XmlClass("tests.fist_login"));

XmlTest second_test = new XmlTest();
second_test.setName("Another login check");

List<XmlClass> secondlogin_classes = new ArrayList<XmlClass>();
secondlogin_classes.add(new XmlClass("tests.second_login"));

List<XmlSuite> suites = new ArrayList<XmlSuite>();

But guess what... that won't work either. TestNg does not seem to find the class(es) containing the test methods. The code is being executed successfully but no tests are run, none failed or skipped.

Another thing I tried was to export the testng.xml to a *.jar file and define it's path looking like this:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();

Note: Test tests,jar is located in the project's root. Like <.../loginproject/tests.jar>

The only thing I made work so far is this:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();

tng.setDefaultTestName("Login check");
tng.setDefaultSuiteName("The Login suite");
tng.setTestClasses(new Class[] { fist_login.class });


This however does not fit my project requirement which is to create a test suite like that shown in the XML above. I need 1 Test Suite, with different login tests which are implemented each in their own class. Also the problem with the last solution is that every class I add to the list is being executed at once but I need to run them after another.

I'd favor a solution that allows me do directly execute the custom testng.xml but I'd also be happy if someone could tell me what I am doing wrong in creating the virtual XML file.

Thanks everyone in advance.

I have come up with the solution where I add my suit file to a list and in turn addd that list via API method to TestNGs' suite list.
Looks like this:

List<String> testFilesList = new ArrayList<String>();
testFilesList.add("./testng.xml"); //test suite resides in the working directory's root folder
**testng.setTestSuites(testFilesList);** //you can addd multiple suites either here by adding multiple files or include all suites needed in the testng.xml file

Answer Source

With the virtual xml example, you have missed adding the class to the test and the test to your suite.