Running TestNG programatically

471 views
Skip to first unread message

Fab

unread,
May 10, 2011, 12:33:08 PM5/10/11
to testng-users
When I run TestNG programatically with following code, only the first
test (i.e. 'Location') is run; the second one 'Routing' is never run.
I use TestNG version 6.0.1

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

suite.setObjectFactory(SingletonObjectFactory.instance());

// Location
XmlTest location = new XmlTest(suite);
location.setName("Location");

List<XmlClass> locationClasses = new ArrayList<XmlClass>();
locationClasses.add(new XmlClass("com.mycompany.Location"));

location.setXmlClasses(locationClasses);

// Routing
XmlTest routing = new XmlTest(suite);
routing.setName("Routing");

List<XmlClass> routingClasses = new ArrayList<XmlClass>();
routingClasses.add(new XmlClass("com.mycompany.Routing"));

routing.setXmlClasses(routingClasses);

TestNG testNG = new TestNG();
List<XmlSuite> xmlsuites = new ArrayList<XmlSuite>();
xmlsuites.add(suite);
testNG.setXmlSuites(xmlsuites);

testNG.run();


A workaround is to provide the test XML file:

// code building 'suite' is not affected

suite.setFileName(suite.getName());

TestNG testNG = new TestNG();

List<String> xmlsuites = new ArrayList<String>();

File file = File.createTempFile(suite.getFileName(), ".xml");

FileWriter writer = new FileWriter(file);
writer.write(suite.toXml());
writer.close();

xmlsuites.add(file.getAbsolutePath());

testNG.setTestSuites(xmlsuites);

I assume the problem may be somewhere in SuiteRunner ?

Fab

Cédric Beust ♔

unread,
May 10, 2011, 12:57:07 PM5/10/11
to testng...@googlegroups.com
Two thoughts:

suite.setObjectFactory(SingletonObjectFactory.instance());

What does instance() return? This should be a String.

Second, can you invoke toXml() on the suite you created programmatically and make sure it looks correct?

-- 
Cédric





--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Fab

unread,
May 11, 2011, 3:19:07 AM5/11/11
to testng-users
Cédric,

Yes, indeed I invoked toXml() to get the XML to write to the temporary
file.
Here is the XML, which seems correct:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite object-factory="com.mycompany.SingletonObjectFactory" name="My
suite">
<parameter name="alice.lastName" value="Hudson"/>
<parameter name="alice.login" value="alice"/>
<parameter name="alice.firstName" value="Alice"/>
<test name="Location" preserve-order="false">
<classes>
<class name="com.mycompany.Location"/>
</classes>
</test>
<test name="Routing" preserve-order="false">
<classes>
<class name="com.mycompany.Routing"/>
</classes>
</test>
</suite>

XmlSuite.setObjectFactory(IObjectFactory objectFactory) takes an
instance of a class implementing the IObjectFactory interface.
As this factory returns singleton objects, I don't want TestNG to
create a new instance of this factory, but rather provide the unique
instance of the factory to TestNG.

Fab

Cédric Beust ♔

unread,
May 11, 2011, 3:24:16 AM5/11/11
to testng...@googlegroups.com
This looks correct. Can you create a small compilable project with this file and the two classes and email it to me?

Thanks.

-- 
Cédric




On Wed, May 11, 2011 at 12:19 AM, Fab <fabien...@hotmail.fr> wrote:T

Fab

Message has been deleted

Rao

unread,
Nov 1, 2012, 5:54:58 AM11/1/12
to testng...@googlegroups.com


On Thursday, November 1, 2012 9:54:12 AM UTC, Rao wrote:
Hello Cedric

I am trying to run TestNg programatically, below is the sample Java file to run the TestNG. while running this i need to alter my MySuite.xml and need to save this file to my project path, 
And also please advise is this possible to save the Virtual TestNG.xml created during the runtime, 
the reason for this to make XSLT reports i need this TestNG.xml to use it from the command prompt.
package testng.samples;

import java.io.File;
import java.util.List;

import org.testng.TestNG;
import org.testng.collections.Lists;

public class Execution {

    public static void main(String[] args) {
        TestNG testng = new TestNG();
        String fileLoc = TestNG.class.getProtectionDomain().getCodeSource().getLocation().getFile();
        System.out.println("TestNG version = " + new File(fileLoc).getParentFile().getName());
        List<String> suites = Lists.newArrayList();
        suites.add("src\\MySuite.xml");
        testng.setTestSuites(suites);
        testng.setPreserveOrder(true);
        testng.run();
    }

}
please advise me about this
Advanced Thanks

Rao 
Reply all
Reply to author
Forward
0 new messages