【问题标题】:How to run TestNG tests pointed to a jar如何运行指向 jar 的 TestNG 测试
【发布时间】:2016-06-12 12:33:26
【问题描述】:

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

【问题讨论】:

    标签: java testng


    【解决方案1】:

    您可以使用 -xmlpathinjar suites/GroupBased_Tests.xml 选项来运行您的 xml。
    如果有帮助,可以参考 maven here 的步骤。

    有关您可以使用的其他选项,请参阅 Testng 文档here

    【讨论】:

    • 您想以编程方式执行 jar 吗?
    • 是的。我有一个从 maven build 生成的 jar 文件。假设我的 jar 文件名为 myproject-1.0-SNAPSHOT 这个 jar 包含我所有的 src/target 文件夹资源。所以我想执行我的 @ 987654326@ 位于生成的 jar 文件中。我想以编程方式进行。例如:用户将文件名作为 arg 的 jar 提供并以编程方式执行该测试。executeTest(.jarfilelocation,fileNametobeExecuted).inside 这个方法测试将运行。
    • 这可能会有所帮助..stackoverflow.com/questions/1320476/… 将是一个单独的问题
    • 谢谢。据您所知,TestNG 中是否有任何工具可以以编程方式对 jar 进行测试?
    • 您也可以在 testng 中以编程方式运行您的测试,而无需打包在 jar 中,如果您要这样做的话
    【解决方案2】:

    您可以阅读 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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      jar 只是一个 zip 文件。

      您可以使用jar xf testfile.jar 提取该文件并访问您想要的文件。


      这个答案也可能对你有所帮助。

      Read a file from inside of an external Jar file?

      【讨论】:

        猜你喜欢
        • 2013-05-04
        • 2017-12-31
        • 1970-01-01
        • 2017-08-19
        • 2019-04-12
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 2023-03-10
        相关资源
        最近更新 更多