Search code examples
javamaventestngdataprovider

Maven + TestNG Print parameters of @DataProvider


The question is simple but I can't find it anywhere (googling) I want to run mvn test and see in the console output the next text "PASSED: theNameOfMyTest("A String Attribute")" the code that will produce this example would look something like this:

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

}

Solution

  • You can use your own Listener which will display the expected information. Then, configure surefire to use it: 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()) + ")");
      }
    }
    

    In your 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>
    [...]