简体   繁体   English

如何运行指向罐子的TestNG测试

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

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

You can use the -xmlpathinjar suites/GroupBased_Tests.xml option to run your xml. 您可以使用-xmlpathinjar suites / GroupBased_Tests.xml选项来运行xml。
Can refer steps with maven here , if it helps. 如果有帮助,可以在这里参考使用maven的步骤。

For other options you can use, please refer Testng documentation here . 有关您可以使用的其他选项,请参阅此处的 Testng文档。

jar is a just a zip file. jar只是一个zip文件。

You can get that extracted using jar xf testfile.jar and access the file you want. 您可以使用jar xf testfile.jar将其提取并访问所需的文件。


This answer also might help you. 这个答案也可能对您有帮助。

Read a file from inside of an external Jar file? 从外部Jar文件中读取文件?

You can read the testng.xml and also run it programmatically without extracting the jar. 您可以读取testng.xml并以编程方式运行它,而无需提取jar。

First you need to read the xml and parse it. 首先,您需要阅读xml并进行解析。 Here I have created some bean classes to match with my xml file format. 在这里,我创建了一些bean类来与我的xml文件格式匹配。 This is my bean class, create you own set of classes which match with your requirement 这是我的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;
}
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从可执行jar中的main()运行TestNG测试? - How to run TestNG tests from main() in an executable jar? Selenium TestNG:如何使用TestNG从编译的jar运行selenium测试? - Selenium TestNG: How can I run selenium tests from a compiled jar using TestNG? 如何在Jenkins上运行TestNG测试 - How to Run TestNG Tests on Jenkins 如何以编程方式使用TestNG运行Selenium Java测试? - How to run Selenium Java tests with TestNG programmatically? 如何通过使用 TestNG 和 Selenium 的方法并行运行测试 - How to run tests in parallel by methods with TestNG and Selenium 如何使用Eclipse并行运行TestNG测试? - How to run TestNG tests in parallel using Eclipse? 如何在libGDX项目中运行TestNG测试? - How to run TestNG tests in libGDX project? TestNG:顺序运行测试 - TestNG : run tests sequentially 我的Maven项目如何在没有TestNG尝试运行JUnit测试的情况下同时运行JUnit和TestNG? - How can my Maven project run both JUnit and TestNG without TestNG trying to run JUnit tests? 如何在不解压依赖项jar的情况下使用具有故障保护功能的TestNG套件来运行? - How to run TestNG suite with failsafe without unpacking dependency jar?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM