如何运行指向 jar 的 TestNG 测试

编程入门 行业动态 更新时间:2024-10-25 16:19:50
本文介绍了如何运行指向 jar 的 TestNG 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 .Jar 文件,其中包含在 TestNG 测试上运行所需的文件.我想在该 Jar 文件中运行特定的 xml 文件.我的要求是可以执行TestNG 测试指向一个 .Jar 文件,如果是这样,我该怎么做.

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.

推荐答案

您可以读取 testng.xml 并以编程方式运行它,而无需提取 jar.

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

首先你需要读取xml并解析它.在这里,我创建了一些 bean 类以匹配我的 xml 文件格式.这是我的 bean 类,创建您自己的一组符合您要求的类

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

最后我们运行套件:

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

这篇关于如何运行指向 jar 的 TestNG 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 22:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1401119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   jar   TestNG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!