2016-02-29 80 views
6

我有一个.Jar文件包含需要在TestNG test.I上运行的文件需要在该JAR文件中运行一个特定的XML文件。我的要求是有可能执行TestNG测试指向.Jar文件,如果有的话我该怎么做。如何运行TestNG测试指向一个罐子

回答

1

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

Thanks.it正常工作正常。 – gihan

2

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

对于您可以使用的其他选项,请参考Testng文档here

+0

我可以执行编程而不是命令行吗? – gihan

+0

你想以编程方式执行jar吗? –

+0

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