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" >
<classes>
<class name="tests.first_login"/>
</classes>
</test>
<test name="Another login check" >
<classes>
<class name="tests.second_login"/>
</classes>
</test>
</suite>
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();
tng.addListener(tla);
XmlSuite loginSuite = new XmlSuite();
loginSuite.setName("The Login Suite");
XmlTest first_test = new XmlTest();
first_test.setName("Some login check");
first_test.setSuite(loginSuite);
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");
loginSuite.addTest(SOEtest);
List<XmlClass> secondlogin_classes = new ArrayList<XmlClass>();
secondlogin_classes.add(new XmlClass("tests.second_login"));
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(loginSuite);
tng.setXmlSuites(suites);
tng.run();
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();
tng.setXmlPathInJar("tests.jar");
tng.addListener(tla);
tng.run();
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 });
tng.addListener(tla);
tng.run();
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.
UPDATE AND SOLUTION: 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
testng.setUseDefaultListeners(false);
testng.addListener(htmlRep);
testng.run();
public static void main(String[] args) {
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
suites.add(".//TestNG.xml");
testng.setTestSuites(suites);
testng.run();
}