Java TestNG-如何在TestNG自定义电子邮件报告中打印运行时TestNG参数?
我发现有一个选项可以通过surefire插件将参数设置为testng xml,然后可以从命令行发送参数Java TestNG-如何在TestNG自定义电子邮件报告中打印运行时TestNG参数?,java,testng,maven-surefire-plugin,testng.xml,Java,Testng,Maven Surefire Plugin,Testng.xml,我发现有一个选项可以通过surefire插件将参数设置为testng xml,然后可以从命令行发送参数 <plugins> [...] <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version&
<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>
预期产出:
[RemoteTestNG] detected TestNG version 7.0.0
chrome
key : value
browser : chrome
===============================================
Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Testng.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参数。假设您使用以下命令行参数运行:
<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>
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>
org.apache.maven.plugins
maven surefire插件
3.0.0-M3
火狐
testng.xml
编辑:
仅供参考,
System.grtProperties()
列出了所有属性,从命令行设置的属性将在那里,但是如果您使用命令行参数运行,则无法将这些属性与系统添加的其他属性区分开来,例如
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>
org.apache.maven.plugins
maven surefire插件
3.0.0-M3
火狐
testng.xml
编辑:
仅供参考,
System.grtProperties()
列出了所有属性,从命令行设置的属性将在那里,但是无法将它们与系统添加的其他属性区分开来我想打印参数列表(键值对),而不指定参数名称(即键),如下所示。Map allParameters=context.getCurrentXmlTest().getAllParameters();对于(字符串参数:allParameters.keySet()){System.out.println(参数+”:“+allParameters.get(参数));}@sandepnalla,我不知道没有它的键如何获取参数。我相信您需要指定检索值的键@Parameters({“param name”})
需要一个键。另一个选项是使用System.getProperties()
,但它会获取系统上JVM从操作系统获取的当前属性。当前系统属性作为properties对象返回,供getProperties()
方法使用。如果不存在这样的属性集,则首先创建一组系统,然后初始化。我希望打印参数列表(键值对),而不指定参数名称(即键),如下所示。Map allParameters=context.getCurrentXmlTest().getAllParameters();对于(字符串参数:allParameters.keySet()){System.out.println(参数+”:“+allParameters.get(参数));}@sandepnalla,我不知道没有它的键如何获取参数。我相信您需要指定检索值的键@Parameters({“param name”})
需要一个键。另一个选项是使用System.getProperties()
,但它会获取系统上JVM从操作系统获取的当前属性。当前系统属性作为properties对象返回,供getProperties()
方法使用。如果不存在这样的属性集,则首先创建一组系统,然后初始化。