I have maven + testng project as below:
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);
}
}
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:
[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?
PS: I found here is related topic here: 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:
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.
So you have two options:
@Parameters
is used) then you stick to TestNG suite xml files. -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
] This is possible because TestNG lets you pass values to @Parameters
via JVM arguments.
For more details please refer to my blog post: https://rationaleemotions.com/building_dynamic_testng_suites/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.