8

I am having a .Jar file that contains the files needed to runs on TestNG test.I want to run a specific xml file inside that Jar file.My requirement is that is it possible to execute TestNG test pointed to a .Jar file if so how can i do that.

gihan-maduranga
  • 4,381
  • 5
  • 41
  • 74

3 Answers3

2

You can use the -xmlpathinjar suites/GroupBased_Tests.xml option to run your xml.
Can refer steps with maven here, if it helps.

For other options you can use, please refer Testng documentation here.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • you want to execute the jar programmatically? – niharika_neo Mar 01 '16 at 05:51
  • yes.i have a jar file that generated from maven build.let say my jar file name is `myproject-1.0-SNAPSHOT` this jar contains all of my `src/target` folder resources.so i want to execute my `testngTestfile.xml` that is located inside that generated jar file.i want to do it programmatically.Eg:user give jar with file name as a arg and execute that test programmatically.`executeTest(.jarfilelocation,fileNametobeExecuted)`.inside this method test will be run. – gihan-maduranga Mar 01 '16 at 05:59
  • 1
    Probably this can help..http://stackoverflow.com/questions/1320476/execute-another-jar-in-a-java-program..that would be a separate question then – niharika_neo Mar 01 '16 at 06:02
  • thanks.As far as u know is there any facility in TestNG to run test against a jar programmatically? – gihan-maduranga Mar 01 '16 at 06:08
  • you can run your tests in testng programmatically too without packaging in a jar, if that is what you are asking – niharika_neo Mar 01 '16 at 06:16
  • no.can we run test against a given jar file programmatically?.or do i need to extract jar and give physical file into TestNG? – gihan-maduranga Mar 01 '16 at 06:19
  • No the above answer would help there..the link that i shared above – niharika_neo Mar 01 '16 at 07:00
1

jar is a just a zip file.

You can get that extracted using jar xf testfile.jar and access the file you want.


This answer also might help you.

Read a file from inside of an external Jar file?

Community
  • 1
  • 1
vins
  • 15,030
  • 3
  • 36
  • 47
1

You can read the testng.xml and also run it programmatically without extracting the jar.

First you need to read the xml and parse it. Here I have created some bean classes to match with my xml file format. This is my bean class, create you own set of classes which match with your requirement

@XmlRootElement(name = "suite")
public class Suite {

private String name;
private String verbose = "1";
private boolean parallel =false;

private List<Test> testCases = new ArrayList<Test>();
private List<Parameter> parameters = new ArrayList<Parameter>();

@XmlAttribute
public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}

@XmlAttribute
public String getVerbose() {
    return verbose;
}


public void setVerbose(String verbose) {
    this.verbose = verbose;
}

@XmlAttribute
public boolean isParallel() {
    return parallel;
}


public void setParallel(boolean parallel) {
    this.parallel = parallel;
}

@XmlElement(name = "test")
public List<Test> getTestCases() {
    return testCases;
}

public void setTestCases(List<Test> testCases) {
    this.testCases = testCases;
}

@XmlElement(name = "parameter")
public List<Parameter> getParameters() {
    return parameters;
}


public void setParameters(List<Parameter> parameters) {
    this.parameters = parameters;
}
}

And this is how you read and parse it:

public Suite getTestSuiteFromJar(String jarFilePath, String filename) {
    File jarFile  = new File(jarFilePath);
    Suite suite = null;
    try {
        if (jarFile.isFile()) {
            final JarFile jar = new JarFile(jarFile);

            InputStream in = jar.getInputStream(new ZipEntry(filename));
            suite = XmlUtil.parseSuite(in);
            jar.close();
        }

    } catch (IOException | JAXBException | SAXException | ParserConfigurationException e) {
        e.printStackTrace();
    }
    return suite;
}

public static Suite parseSuite(InputStream is) throws JAXBException, SAXException, ParserConfigurationException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Suite.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (Suite) jaxbUnmarshaller.unmarshal(is);
}

Finally we run the suite:

public static void runSuite(String jarFilePath, Suite s)
        throws MalformedURLException, ClassNotFoundException {
    //Don't confuse : XmlSuite here, is the standard testNg class. our bean class is Suite
    XmlSuite suite = new XmlSuite();
    suite.setName(s.getName());

    for (Test t : s.getTestCases()) {
        XmlTest test = new XmlTest(suite);
        test.setName(t.getName());
        List<XmlClass> classes = new ArrayList<XmlClass>();
        for (TestClass tc : t.getClasses()) {
            Class cls =  loadClass(jarFilePath, tc.getName());
            if (cls != null) {
                XmlClass xClass = new XmlClass(cls, false);
                classes.add(xClass);
                test.setXmlClasses(classes);
            }
        }
    }
    List<XmlSuite> suites = new ArrayList<XmlSuite>();

    suites.add(suite);
    TestNG tng = new TestNG();

    tng.setXmlSuites(suites);
    tng.run();
}

public Class loadClass(String jarFilePath, String className) throws MalformedURLException,
        ClassNotFoundException {
File jarFile  = new File(jarFilePath);
    if (jarFile.isFile()) {
        URL url = jarFile.toURL();
        URL[] urls = new URL[] { url };
        ClassLoader cl = new URLClassLoader(urls);
        return cl.loadClass(className);
    } else {
        return null;
    }
}
Tharaka Deshan
  • 1,349
  • 3
  • 15
  • 30