java - Maven + TestNG 打印@DataProvider的参数

标签 java maven testng dataprovider

问题很简单,但我在任何地方都找不到它(谷歌搜索)我想运行 mvn test 并在控制台输出中看到下一个文本“PASSED: theNameOfMyTest("A String Attribute")"生成此示例的代码将如下所示:

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);
    }

}

最佳答案

您可以使用自己的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>
[...]

关于java - Maven + TestNG 打印@DataProvider的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383744/

相关文章:

java - 如何将 Artifact 和所有依赖项递归下载到目录中?

Java 列表按引用或值传递

java - 是否有与 C++ 的 std::map 等效的 Java Map keySet()?

java - 多语言集成测试框架

java - maven项目中的Classloader.getResources()返回一个空的Enumeration

maven - 使用(maven)存储库和 Maven/Gradle 管理多个项目的最佳实践?

java.lang.IllegalStateException : Failed to load ApplicationContext when running unit test with HSQL embedded database

java - DependsOnGroups 因循环依赖异常而失败

java - 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 配合使用

java - 当范围标记为 'test' 时导入 testng 类