簡體   English   中英

運行特定測試類的Maven命令得到了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命令以運行諸如“ 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 ”,但是沒有不起作用,如何使其按預期工作?

PS:我在這里發現了相關主題: https : //groups.google.com/forum/#!msg/ testng-users/ ccp_ewuNWlk/kmMXi0ycAwAJ

您正在混合兩種執行模式。 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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM