最初,我有一个空白的testng.xml文件,所以我正在从excel套件文件中动态写入该文件。但是,当我从Jenkins或本地pom.xml文件执行此文件时,出现以下异常。因此,有一种方法,我们首先写入testNG.xml文件,然后从POM调用它。看来我需要一种在@Before套件之前执行的方法。
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building restautomation 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4:enforce (enforce-plugin-versions) @ restautomation ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ restautomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\C47273\Xen Desktop\Workspaces\restautomation\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ restautomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ restautomation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\C47273\Xen Desktop\Workspaces\restautomation\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ restautomation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ restautomation ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.083 s
[INFO] Finished at: 2019-07-18T16:51:56+02:00
[INFO] Final Memory: 16M/225M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project restautomation: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test failed: There was an error in the forked process
[ERROR] org.testng.TestNGException: org.xml.sax.SAXParseException; Premature end of file.
[ERROR] at org.testng.TestNG.parseSuite(TestNG.java:327)
[ERROR] at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:348)
[ERROR] at org.testng.TestNG.initializeEverything(TestNG.java:995)
[ERROR] at org.testng.TestNG.run(TestNG.java:1009)
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:281)
[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
[ERROR] Caused by: org.xml.sax.SAXParseException; Premature end of file.
[ERROR] at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
[ERROR] at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
[ERROR] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
[ERROR] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
[ERROR] at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
[ERROR] at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
[ERROR] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[ERROR] at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
[ERROR] at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
[ERROR] at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
[ERROR] at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
[ERROR] at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
[ERROR] at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
[ERROR] at org.testng.xml.XMLParser.parse(XMLParser.java:38)
[ERROR] at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16)
[ERROR] at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9)
[ERROR] at org.testng.xml.Parser.parse(Parser.java:152)
[ERROR] at org.testng.xml.Parser.parse(Parser.java:233)
[ERROR] at org.testng.TestNG.parseSuite(TestNG.java:295)
[ERROR] ... 9 more
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
答案 0 :(得分:0)
您应该可以利用org.testng.IAlterSuiteListener
来做到这一点。
下面是一个示例,展示了如何执行此操作(为简便起见,我使用的是json文件而不是excel电子表格,但是您应该可以用支持Excel电子表格的解决方案替换此解决方案)
[
{
"class": "com.rationaleemotions.stackoverflow.qn57097760.ThisIsSampleClassOne"
}
]
这里是Testng侦听器
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.testng.IAlterSuiteListener;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class TestBuildingListener implements IAlterSuiteListener {
@Override
public void alter(List<XmlSuite> suites) {
XmlSuite xmlsuite = suites.get(0);
try {
createXmlTest(xmlsuite);
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
private void createXmlTest(XmlSuite xmlsuite) throws FileNotFoundException {
XmlTest xmltest = new XmlTest(xmlsuite);
xmltest.setName(xmlsuite.getName() + "_test");
List<XmlClass> xmlClasses = new ArrayList<>();
for (String clazz : getClassNames()) {
xmlClasses.add(new XmlClass(clazz));
}
xmltest.setXmlClasses(xmlClasses);
}
private List<String> getClassNames() throws FileNotFoundException {
JsonArray array =
new JsonParser().parse(new FileReader("src/test/resources/57097760.json")).getAsJsonArray();
List<String> result = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
result.add(array.get(i).getAsJsonObject().get("class").getAsString());
}
return result;
}
}
这是testng套件xml文件的样子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="57097760_suite" verbose="2">
<listeners>
<listener
class-name="com.rationaleemotions.stackoverflow.qn57097760.TestBuildingListener"/>
</listeners>
</suite>
这是测试类的样子
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class ThisIsSampleClassOne {
@Test
public void a() {
display();
}
@Test
public void b() {
display();
}
private void display() {
ITestResult result = Reporter.getCurrentTestResult();
String m = result.getMethod().getMethodName() + "()";
String c = result.getTestClass().getRealClass().getName();
System.err.println(c + "." + m);
}
}
以下是输出:
... TestNG 7.0.0-beta7 by Cédric Beust (cedric@beust.com)
...
com.rationaleemotions.stackoverflow.qn57097760.ThisIsSampleClassOne.a()
com.rationaleemotions.stackoverflow.qn57097760.ThisIsSampleClassOne.b()
PASSED: a
PASSED: b
===============================================
57097760_suite_test
Tests run: 2, Failures: 0, Skips: 0
===============================================
===============================================
57097760_suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0