gpt4 book ai didi

java - 我有一个在运行时动态生成的 testNG.xml 文件

转载 作者:行者123 更新时间:2023-12-02 02:52:40 34 4
gpt4 key购买 nike

最初我有一个空白的 testng.xml 文件,所以我从我的 Excel 套件文件动态写入该文件。但是当我从 Jenkins 或本地 pom.xml 文件执行此文件时,出现以下异常。那么有没有一种方法可以让我们首先写入 testNG.xml 文件,然后从 POM 调用它。这看起来像我需要在 @Before suite 之前执行一种方法。

[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

最佳答案

您应该能够通过利用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

关于java - 我有一个在运行时动态生成的 testNG.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57097760/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com