如何运行TestNG测试指向一个jar

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

您可以阅读testng.xml并以编程方式运行它而无需解压缩jar。

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

@XmlRootElement(name = "suite") public class Suite { private String name; private String verbose = "1"; private boolean parallel =false; private List testCases = new ArrayList(); private List parameters = new ArrayList(); @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 getTestCases() { return testCases; } public void setTestCases(List testCases) { this.testCases = testCases; } @XmlElement(name = "parameter") public List getParameters() { return parameters; } public void setParameters(List 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 classes = new ArrayList(); 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 suites = new ArrayList(); 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; } } 

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

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

jar只是一个zip文件。

您可以使用jar xf testfile.jar获取该文件并访问所需的文件。


这个答案也可以帮到你。

从外部Jar文件中读取文件?