maven 命令 运行 特定测试 class 得到 org.testng.TestNGException
maven command to run specific test class got org.testng.TestNGException
我有 maven + testng 项目如下:
pom.xml
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/config/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
testng.xml
<test name="SendAuroraRequests_TEST">
<parameter name="requestsToEnv" value="test" />
<classes>
<class name="com.test.TrackerTest" />
</classes>
</test>
TrackerTest.java
package com.test;
public class TrackerTest {
private ITestContext context;
@Parameters("requestsToEnv")
@BeforeTest
public void setInvocationCount(ITestContext context, String requestsToEnv){
this.context = context;
this.setInvocationCount(context, this, requestsToEnv);
}
}
当我尝试 运行 "mvn test" 命令时效果很好,但是当我尝试 maven 命令 运行 特定测试时 class 像“mvn test -Dtest=TrackerTest”,它会抛出像这样的异常:
[ERROR] setInvocationCount(com.test.TrackerTest) Time elapsed: 0.656 s <<< FAILURE!
org.testng.TestNGException:
Parameter 'requestsToEnv' is required by BeforeTest on method setInvocationCount but has not been marked @Optional or defined
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TrackerTest.setInvocationCount ? TestNG
Parameter 'requestsToEnv' is re...
[INFO]
[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 3
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.838 s
[INFO] Finished at: 2019-08-09T22:56:34+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project hfatest-tracker: There are test failures.
看起来 运行 特定测试 class 使用 maven 命令不会尝试从 testng.xml 获取参数,我也尝试使用类似“mvn test - Dtest=TrackerTest -DsuiteXmlFile=src/test/resources/config/testng.xml" 但没有用,如何让它按预期工作?
P.S: 我在这里找到相关主题:
https://groups.google.com/forum/#!msg/testng-users/ccp_ewuNWlk/kmMXi0ycAwAJ
您混合了两种执行模式。 TestNG 允许您 运行 在两种模式下进行测试:
- 通过 TestNG 套件 xml 文件
- 通过指定它们的完全限定 class 名称来单独测试 classes。
您应该尝试只使用其中一种模式,不要混合使用它们。
当您 运行 通过 -Dtest
传递单个测试 class 时,TestNG 会创建一个没有任何参数的命令行套件。
所以你有两个选择:
- 当您的测试涉及参数(使用
@Parameters
)时,您会坚持使用 TestNG 套件 xml 文件。
- 如果您仍然希望通过
-Dtest
JVM 参数 运行 单独测试 classes 那么您可以通过 JVM 参数传递参数值 [所以在您的如果是 mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test
]
这是可能的,因为 TestNG 允许您通过 JVM 参数将值传递给 @Parameters
。
更多详情请参考我的博客post:https://rationaleemotions.com/building_dynamic_testng_suites/
我有 maven + testng 项目如下:
pom.xml
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/config/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
testng.xml
<test name="SendAuroraRequests_TEST">
<parameter name="requestsToEnv" value="test" />
<classes>
<class name="com.test.TrackerTest" />
</classes>
</test>
TrackerTest.java
package com.test;
public class TrackerTest {
private ITestContext context;
@Parameters("requestsToEnv")
@BeforeTest
public void setInvocationCount(ITestContext context, String requestsToEnv){
this.context = context;
this.setInvocationCount(context, this, requestsToEnv);
}
}
当我尝试 运行 "mvn test" 命令时效果很好,但是当我尝试 maven 命令 运行 特定测试时 class 像“mvn test -Dtest=TrackerTest”,它会抛出像这样的异常:
[ERROR] setInvocationCount(com.test.TrackerTest) Time elapsed: 0.656 s <<< FAILURE!
org.testng.TestNGException:
Parameter 'requestsToEnv' is required by BeforeTest on method setInvocationCount but has not been marked @Optional or defined
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] TrackerTest.setInvocationCount ? TestNG
Parameter 'requestsToEnv' is re...
[INFO]
[ERROR] Tests run: 4, Failures: 1, Errors: 0, Skipped: 3
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.838 s
[INFO] Finished at: 2019-08-09T22:56:34+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project hfatest-tracker: There are test failures.
看起来 运行 特定测试 class 使用 maven 命令不会尝试从 testng.xml 获取参数,我也尝试使用类似“mvn test - Dtest=TrackerTest -DsuiteXmlFile=src/test/resources/config/testng.xml" 但没有用,如何让它按预期工作?
P.S: 我在这里找到相关主题: https://groups.google.com/forum/#!msg/testng-users/ccp_ewuNWlk/kmMXi0ycAwAJ
您混合了两种执行模式。 TestNG 允许您 运行 在两种模式下进行测试:
- 通过 TestNG 套件 xml 文件
- 通过指定它们的完全限定 class 名称来单独测试 classes。
您应该尝试只使用其中一种模式,不要混合使用它们。
当您 运行 通过 -Dtest
传递单个测试 class 时,TestNG 会创建一个没有任何参数的命令行套件。
所以你有两个选择:
- 当您的测试涉及参数(使用
@Parameters
)时,您会坚持使用 TestNG 套件 xml 文件。 - 如果您仍然希望通过
-Dtest
JVM 参数 运行 单独测试 classes 那么您可以通过 JVM 参数传递参数值 [所以在您的如果是mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test
]
这是可能的,因为 TestNG 允许您通过 JVM 参数将值传递给 @Parameters
。
更多详情请参考我的博客post:https://rationaleemotions.com/building_dynamic_testng_suites/