简体   繁体   English

运行特定测试类的Maven命令得到了org.testng.TestNGException

[英]maven command to run specific test class got org.testng.TestNGException

I have maven + testng project as below: 我有如下的Maven + Testng项目:

pom.xml 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 testng.xml

<test name="SendAuroraRequests_TEST">
    <parameter name="requestsToEnv" value="test" />
    <classes>
        <class name="com.test.TrackerTest" />
    </classes>
</test>

TrackerTest.java 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);
    }
}

It works well when I try to run " mvn test " command, but when I try maven command to run specific test class like " mvn test -Dtest=TrackerTest ", it throws exception like: 当我尝试运行“ 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.

Looks run specific test class with maven command aren't try to get parameter from testng.xml, I also tried with command like " mvn test -Dtest=TrackerTest -DsuiteXmlFile=src/test/resources/config/testng.xml " but didn't work, how to make it works as expect? 看起来使用maven命令运行特定的测试类并没有尝试从testng.xml获取参数,我也尝试了使用类似的命令“ mvn test -Dtest = TrackerTest -DsuiteXmlFile = src / test / resources / config / testng.xml ”,但是没有不起作用,如何使其按预期工作?

PS: I found here is related topic here: https://groups.google.com/forum/#!msg/testng-users/ccp_ewuNWlk/kmMXi0ycAwAJ PS:我在这里发现了相关主题: https : //groups.google.com/forum/#!msg/ testng-users/ ccp_ewuNWlk/kmMXi0ycAwAJ

You are mixing the two modes of execution. 您正在混合两种执行模式。 TestNG lets you run tests in two modes: TestNG允许您以两种模式运行测试:

  • Via a TestNG suite xml file 通过TestNG套件xml文件
  • Individual test classes by specifying the fully qualified class names of them. 通过指定测试类的完全限定的类名来进行测试。

You should try to use ONLY one of these modes and NOT mix them. 您应该尝试仅使用这些模式之一,而不要混用它们。

When you run your tests by passing in individual test classes via the -Dtest TestNG creates a command line suite, which would not have any parameters. 通过-Dtest传递各个测试类来运行测试时,TestNG将创建一个命令行套件,该套件不包含任何参数。

So you have two options: 因此,您有两种选择:

  1. When your tests involves parameters ( @Parameters is used) then you stick to TestNG suite xml files. 如果您的测试涉及参数(使用@Parameters ),那么您将坚持使用TestNG套件xml文件。
  2. If you still would like to run individual test classes via the -Dtest JVM argument then you can pass in the values of the parameters through JVM arguments [ so in your case it would be mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test ] 如果您仍然想通过-Dtest JVM参数运行单个测试类,则可以通过JVM参数传递参数的值[因此,在您的情况下,它将是mvn clean test -Dtest=TrackerTest -DrequestsToEnv= test ]

This is possible because TestNG lets you pass values to @Parameters via JVM arguments. 这是可能的,因为TestNG允许您通过JVM参数将值传递给@Parameters

For more details please refer to my blog post: https://rationaleemotions.com/building_dynamic_testng_suites/ 有关更多详细信息,请参阅我的博客文章: https : //rationaleemotions.com/building_dynamic_testng_suites/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM