Maven + TestNG打印@DataProvider的参数

时间:2016-09-08 06:17:06

标签: java maven testng dataprovider

问题很简单,但我无法在任何地方找到它(谷歌搜索)我想运行mvn test并在控制台输出中看到下一个文字" PASSED: theNameOfMyTest("A String Attribute")&#34 ;产生这个例子的代码看起来像这样:

import static org.testng.Assert.assertTrue;

public class TestClass {

    @DataProvider(name = "provider")
    public static Object[][] provider() {
        return new Object[][] {{"A String Attribute"}};
    }

    @Test(dataProvioder="provider")
    public void theNameOfMyTest(final String parameter) {
        assertTrue(true);
    }

}

1 个答案:

答案 0 :(得分:2)

您可以使用自己的Listener来显示预期的信息。然后,配置surefire以使用它:https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html

public class MyTestListener extends TestListenerAdapter {

  @Override
  public void onTestFailure(ITestResult tr) {
    log("FAIL: ", tr);
  }

  @Override
  public void onTestSkipped(ITestResult tr) {
    log("SKIPPED: ", tr);
  }

  @Override
  public void onTestSuccess(ITestResult tr) {
    log("PASSED: ", tr);
  }

  private static void log(String prefix, ITestResult tr) {
    System.out.println(prefix + tr.getName() + "(" + Arrays.asList(tr.getParameters()) + ")");
  }
}

pom.xml

[...]
<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <properties>
            <property>
              <name>listener</name>
              <value>MyTestListener</value>
            </property>
          </properties>
        </configuration>
      </plugin>
    [...]
</plugins>
[...]