我发现有一个选项可以通过surefire插件将参数设置为testng xml,然后可以从命令行发送参数。
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertyVariables>
<browser>firefox</browser>
</systemPropertyVariables>
</configuration>
</plugin>
[...]
</plugins>
参考: https ://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html https://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html
需要在 testng 自定义电子邮件报告中打印参数。能够使用以下代码打印 testng xml 中指定的 testng 参数。但是,无法打印 surefire 插件中指定的参数。
注意: System.getProperty("browser") 在这里工作。但是,我想打印它们而不必指定参数名称(比如“浏览器”),如下所示。但低于一个不起作用。
Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
for(String parameter: allParameters.keySet()) {
System.out.println(parameter + " : " + allParameters.get(parameter));
}
例子:
import java.util.Map;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestNGTest {
ITestContext context;
@BeforeTest
public void beforeTest(ITestContext context) {
this.context = context;
}
@Parameters({"browser"})
@Test
public void method(String browser) {
System.out.println(browser);
Map<String, String> allParameters = context.getCurrentXmlTest().getAllParameters();
for(String parameter: allParameters.keySet()) {
System.out.println(parameter + " : " + allParameters.get(parameter));
}
}
}
实际输出:
[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
预期输出:
[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
browser : chrome
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
测试.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes" thread-count="4">
<test name="Front-End" group-by-instances="true">
<parameter name="key" value="value"></parameter>
<classes>
<class name="com.ftd.automation.framework.tests.TestNGTest" />
</classes>
</test>
</suite>
请帮助我了解如何打印在surefire插件中指定或在命令行中传递的testng参数。