2016-02-29 16 views
6

Mam plik .Jar, który zawiera pliki potrzebne do uruchomienia testu TestNG. Chcę uruchomić konkretny plik XML wewnątrz tego pliku Jar.My wymagania jest to, że możliwe jest wykonanie testu TestNG wskazującego na plik .Jar, jeśli tak, jak mogę to zrobić.Jak uruchomić testy TestNG wskazujące na słoik

Odpowiedz

1

Możesz przeczytać plik testng.xml, a także uruchamiać go programowo, nie wyodrębniając słoika.

Najpierw należy przeczytać xml i przeanalizować go. Oto niektóre klasy komponentów bean, które pasują do mojego formatu pliku xml. To moja klasa fasola, tworzyć własne zestaw klas, które odpowiadają z wymogu

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

I to jest, jak czytać i analizować go:

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

Wreszcie możemy uruchomić pakiet:

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

Dzięki. Działa poprawnie zgodnie z oczekiwaniami. – gihan

2

Można użyć -xmlpathinjar apartamentów/option GroupBased_Tests.xml uruchomić xml.
Może odnosić się do czynności z maven here, jeśli to pomaga.

Aby uzyskać informacje o innych opcjach, skorzystaj z dokumentacji Testng here.

+0

Czy mogę wykonywać programowo zamiast wiersza poleceń? – gihan

+0

chcesz programowo uruchamiać jar? –

+0

yes.i mam plik JAR, który wygenerowany z maven build.let mówi, że moja nazwa pliku JAR to 'myproject-1.0-SNAPSHOT' ten jar zawiera wszystkie moje zasoby folderu' src/target', więc chcę wykonać mój plik 'testngTestfile .xml', który znajduje się w tym wygenerowanym pliku jar.i chce to zrobić programowo.Eg: użytkownik poda słoik z nazwą pliku jako arg i wykona ten program programowo ".executeTest (.jarfilelocation, fileNametobeExecuted)' .wtedy ten test metody zostanie uruchomiony. – gihan

Powiązane problemy