首页 > 解决方案 > 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参数。

标签: javatestngmaven-surefire-plugintestng.xml

解决方案


假设您正在使用命令行参数运行,例如,

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()列出所有属性,从命令行设置的属性将在那里,但无法将它们与系统添加的其他属性区分开来


推荐阅读