TestNG: mvn启动测试方案时,xml文件定位失败的问题解决
现象:多module测试项目,跑testng测试xml,使用mvn clean test启动时,报错如下
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19.1:test (default-test) on project trade-test-testng: 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.apache.maven.surefire.testset.TestSetFailedException: Suite file D:\syncRepos\codeJava\GitLabRespo\muji\trade-test\trade-test-testng\src\test\resources\testng\testng-temp.xml is not a valid file[ERROR] at org.apache.maven.surefire.testng.TestNGXmlTestSuite.locateTestSets(TestNGXmlTestSuite.java:98)[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:120)[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)
问题分析:mvn clean启动xml时,build会检查maven-surefire-plugin指定的suiteXmlFile,在编译产物文件里没找到,导致报错。
技术理解:多module的parent和child的pom关系在maven clean时是如何起作用的?现在遇到的问题是父pom定义的suiteXmlFile在子module未指定文件也被要求执行了,结果自然在子module对应路径里找不到资源。显然多module项目的pom关系是,parent的定义会被每个child执行。
问题解决:将父pom里的plugin定义移走,到该文件所处的module pom里增加对应的定义,再次运行,问题搞定!具体pom的build plugin细节如下
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!--<testFailureIgnore>true</testFailureIgnore>-->
<!--argLine: -Dfile.encoding=UTF-8指定surefire运行用例新建jvm的编码参数,否则mvn启动后,输出lf4j2日志乱码-->
<!--forkMode: never为不新建,once是每次都新建-->
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
<suiteXmlFiles>
<!--<suiteXmlFile>testng.xml</suiteXmlFile>-->
<!--suppress UnresolvedMavenProperty -->
<suiteXmlFile>src/test/resources/testng/${TESTXML}</suiteXmlFile>
<!--此处testng.xml即为要运行的testng.xml文件-->
</suiteXmlFiles>
</configuration>
</plugin>