【问题标题】:maven command to run specific test class got org.testng.TestNGException运行特定测试类的 maven 命令得到 org.testng.TestNGException
【发布时间】:2019-12-17 09:17:54
【问题描述】:

我有 ma​​ven + 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" 但没有用,如何让它按预期工作?

P.S:我在这里找到了相关主题: https://groups.google.com/forum/#!msg/testng-users/ccp_ewuNWlk/kmMXi0ycAwAJ

【问题讨论】:

    标签: java maven testng


    【解决方案1】:

    您正在混合两种执行模式。 TestNG 允许您以两种模式运行测试:

    • 通过 TestNG 套件 xml 文件
    • 通过指定它们的完全限定类名来单独测试类。

    您应该尝试只使用其中一种模式,而不是混合使用。

    当您通过-Dtest TestNG 传入单个测试类来运行测试时,会创建一个命令行套件,该套件不包含任何参数。

    所以你有两个选择:

    1. 当您的测试涉及参数(使用@Parameters)时,您将坚持使用 TestNG 套件 xml 文件。
    2. 如果您仍想通过 -Dtest JVM 参数运行单个测试类,那么您可以通过 JVM 参数传入参数的值 [因此在您的情况下为 mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test]

    这是可能的,因为 TestNG 允许您通过 JVM 参数将值传递给 @Parameters

    更多详情请参考我的博文:https://rationaleemotions.com/building_dynamic_testng_suites/

    【讨论】:

    • 非常感谢,它对我有用! [ mvn clean test -Dtest=TrackerTest -DrequestsToEnv=test ] 这就是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多