我有 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命令以运行诸如“ 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.
使用maven命令运行特定的测试类不会尝试从testng.xml中获取参数,我还尝试了使用诸如 mvn test -Dtest = TrackerTest -DsuiteXmlFile = src / test / resources / config / testng.xml ”,但没有用,如何使其按预期工作?
附言::我在这里找到了与此处相关的主题: https://groups.google.com/forum/#!msg/testng-users/ccp_ewuNWlk/kmMXi0ycAwAJ
答案 0 :(得分:0)
您正在混合两种执行模式。 TestNG允许您以两种模式运行测试:
您应该只尝试使用其中一种模式,而不要混用它们。
当您通过-Dtest
传入各个测试类来运行测试时,TestNG将创建一个命令行套件,该套件不包含任何参数。
因此,您有两种选择:
@Parameters
)时,您将坚持使用TestNG套件xml文件。-Dtest
JVM参数运行单个测试类,则可以通过JVM参数传递参数的值[因此,您的情况是mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test
] < / li>
这是可能的,因为TestNG允许您通过JVM参数将值传递给@Parameters
。
有关更多详细信息,请参阅我的博客文章:https://rationaleemotions.com/building_dynamic_testng_suites/