簡體   English   中英

如何運行指向罐子的TestNG測試

[英]How to run TestNG tests pointed to a jar

我有一個.Jar文件,其中包含在TestNG test上運行所需的文件。我想在該Jar文件中運行特定的xml文件。我的要求是,如果可以的話,可以執行指向.Jar文件的TestNG測試我怎樣才能做到這一點。

您可以使用-xmlpathinjar suites / GroupBased_Tests.xml選項來運行xml。
如果有幫助,可以在這里參考使用maven的步驟。

有關您可以使用的其他選項,請參閱此處的 Testng文檔。

jar只是一個zip文件。

您可以使用jar xf testfile.jar將其提取並訪問所需的文件。


這個答案也可能對您有幫助。

從外部Jar文件中讀取文件?

您可以讀取testng.xml並以編程方式運行它,而無需提取jar。

首先,您需要閱讀xml並進行解析。 在這里,我創建了一些bean類來與我的xml文件格式匹配。 這是我的bean類,創建與您的需求匹配的自己的類集

@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;
}
}

這就是您閱讀和解析它的方式:

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);
}

最后,我們運行套件:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM