java - TestNG - 如何在 testng 自定义电子邮件报告中打印运行时 testng 参数?
问题描述
我发现有一个选项可以通过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参数。
解决方案
假设您正在使用命令行参数运行,例如,
mvn test -Dbrowser=firefox
然后获取参数,
import org.testng.annotations.Parameters;
@Parameters("browser")
@Test
public void myTestMethod(String browser){
System.out.println(browser);
}
//or as Test parameter
@Test(parameters = {"browser"})
public void myTestMethod(String browser){
System.out.println(browser);
}
//or System.getProperty() way
@Test
public void myTestMethod(){
System.out.println(System.getProperty("browser"));
}
以上效果很好。另外,如果你需要使用testng.xml
,你可以指定suiteXmlFile
类似的,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertyVariables>
<browser>firefox</browser>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
编辑:
仅供参考,System.grtProperties()
列出所有属性,从命令行设置的属性将在那里,但无法将它们与系统添加的其他属性区分开来
推荐阅读
- javascript - Redux /w 提供者如何添加
- amazon-web-services - Dynamo DB 凭证管理
- python-3.x - 尝试下载mnist时出现持续下载错误
- jakarta-ee - javax.portlet 的 Jakarta EE9 依赖项
- javascript - 这是一个有效的 Javascript 算法来判断一个变量是否是一个素数?
- node.js - 关于使用 Prisma 和 PostgreSQL 对 NestJS 应用程序进行 dockerizing 的问题
- javascript - 向单击按钮的人发送临时消息
- firebase - Onesignal“Apns Delegate Never Fired”在使用 Firebase Auth 时使用和不使用 swizzling
- node.js - 解析graphql查询后实现自定义逻辑
- django - Django:如何在 Postgresql 中对日语(多字节字符串)进行全文搜索