Ho un file .Jar che contiene i file necessari all'esecuzione sul test TestNG. Voglio eseguire un file xml specifico all'interno del file Jar. Il mio requisito è che sia è possibile eseguire test TestNG puntato su un file .Jar se sì, come posso farlo.Come eseguire i test TestNG puntati su un barattolo
risposta
È possibile leggere il testng.xml ed eseguirlo anche a livello di codice senza estrarre il jar.
Per prima cosa è necessario leggere l'xml e analizzarlo. Qui ho creato alcune classi di bean per abbinarle al mio formato di file xml. Questa è la mia classe bean, creare voi proprio set di classi che corrispondono con il vostro requisito
@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;
}
}
E questo è il modo di leggere e analizzarlo:
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);
}
Infine eseguire la suite:
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 è solo un file zip.
È possibile ottenere quello estratto utilizzando jar xf testfile.jar
e accedere al file desiderato.
Questa risposta potrebbe anche aiutare.
È possibile utilizzare le suite -xmlpathinjar/Opzione GroupBased_Tests.xml per eseguire il xml.
Può riferirsi a passaggi con Maven here, se aiuta.
Per altre opzioni che è possibile utilizzare, consultare la documentazione di Testing here.
posso eseguire a livello di codice anziché riga di comando? – gihan
vuoi eseguire il barattolo programmaticamente? –
yes.i ho un file jar generato da maven build.let che dice che il mio nome file jar è 'myproject-1.0-SNAPSHOT' questo jar contiene tutte le mie risorse' src/target' folder.so voglio eseguire il mio 'testngTestfile .xml' che si trova all'interno del file jar generato. Voglio farlo a livello di codice.Eg: l'utente assegna a jar il nome del file come arg ed esegue quel test a livello di codice .executeTest (.jarfilelocation, fileNametobeExecuted) '.In questo test di metodo sarà eseguito. – gihan
Thanks.it funziona bene come previsto. – gihan