我有一个.Jar文件包含需要在TestNG test.I上运行的文件需要在该JAR文件中运行一个特定的XML文件。我的要求是有可能执行TestNG测试指向.Jar文件,如果有的话我该怎么做。如何运行TestNG测试指向一个罐子
回答
您可以阅读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;
}
}
jar是一个zip文件。
您可以使用jar xf testfile.jar
获取该文件并访问您想要的文件。
此答案也可能对您有所帮助。
您可以使用-xmlpathinjar套房/ GroupBased_Tests.xml选项来运行你的XML。
如果有帮助,可以参考maven here的步骤。
对于您可以使用的其他选项,请参考Testng文档here。
我可以执行编程而不是命令行吗? – gihan
你想以编程方式执行jar吗? –
yes.i有一个从maven build.let生成的jar文件,说我的jar文件名是'myproject-1.0-SNAPSHOT'这个jar包含我所有的'src/target'文件夹resources.so我想执行我的'testngTestfile .xml“,它位于生成的jar文件中。我想以编程方式执行它。例如:用户将jar文件名作为arg并以编程方式执行该测试.'executeTest(.jarfilelocation,fileNametobeExecuted)'.inside this method test将会运行。 – gihan
- 1. Maven start从罐子TestNG测试
- 2. java.util.MissingResourceException运行一个罐子
- 3. TESTNG测试运行序列
- 4. 运行TestNG测试实例
- 5. 如何运行sbt-assembly构建的罐子的测试
- 6. 如何从罐子中运行JUnit测试,而JUnit的找不到测试类
- 7. 重新运行TestNG的测试导致测试运行永远
- 8. 运行从另一个罐子的罐子
- 9. 在另一个罐子里面运行外部罐子
- 10. 运行一个罐子两次
- 11. 导出并运行一个罐子
- 12. 运行TestNG的GUI工具测试
- 13. 从.Net运行Java TestNG测试
- 14. 在Maven中运行TestNG测试失败
- 15. 运行多重测试的TestNG套件
- 16. TestNG未在eclipse上运行测试
- 17. 在运行时跳过TestNg测试
- 18. testng多次运行硒测试
- 19. TestNG显示0测试运行
- 20. Selenium TestNG测试运行顺序
- 21. 运行TestNG测试时出错
- 22. 如何使用selenium + testng并行运行测试用例
- 23. 如何使用testng并行运行我的硒测试方法
- 24. 突然无法从ant运行TestNG测试([testng]引发:java.lang.ClassNotFoundException:com.beust.jcommander.ParameterException)
- 25. 无法使用runng运行testng文件作为Testng测试
- 26. Selenium TestNG:如何使用TestNG从编译的jar中运行selenium测试?
- 27. 如何使用TestNG不同数据重新运行测试使用TestNG
- 28. 如何运行从罐子一个maven打包Clojure的应用
- 29. 从单个测试用例中运行多个TestNG测试用例
- 30. 如何强制在maven测试目标期间运行的testNG测试超时?
Thanks.it正常工作正常。 – gihan