testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

Can not set test case name/discription for each of the test case in a @DataProvider #501

Closed asoneji closed 4 years ago

asoneji commented 10 years ago

Can not set test case name/discription for each of the test case in a @DataProvider.

I want to run same test with a @DataProvider. But for each data provider run I would like to update the test description.

I know I could make 2 different data providers and 2 different test case. But that fix will not work for me as I have many entry in the data provider and don't want to make test for each of them. I just want to have different test description for them.

thanks,

ankit

import static org.testng.Assert.assertTrue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.intuit.ctodev.qa.dis.library.util.DataProviderLevel;

public class SampleTest
{
    @Test(groups = { "dptest"}, dataProvider = "dptest")
    public void SampleTest1(String testName)
    {
        System.out.println("\n\nStart sample test - 1 - " + testName);
        assertTrue(true, "test FAILED because of value is not as expected.");
    }

    @DataProvider(name = "dptest")
    public Object[][] dptest()
    {
        return new Object[][]
        {
                { "test1"}, //test description: testing test1
                { "test2"}, //test description: testing test2
        };
    }
}
krmahadevan commented 4 years ago

As of TestNG 7.3.0 below is how the test name can be set. But there is no assurance that the value set here will show up on the reports, because every report is free to use its own mechanism to show the test name. It will get show the value only if ITestResult.getName() is used.

For the description part, its associated with a test method and in the case of a data provider, its the same test method that runs multiple times. So setting the description is going to end up one iteration's value overwriting the other iteration's value.

import java.util.List;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ITestResult;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;

@Listeners(SampleTest.class)
public class SampleTest implements IHookable, IReporter {

  @Test(groups = {"dptest"}, dataProvider = "dptest")
  public void SampleTest1(String testName) {
    System.out.println("\n\nStart sample test - 1 - " + testName);
  }

  @DataProvider(name = "dptest")
  public Object[][] dptest() {
    return new Object[][]{
        {"test1"}, //test description: testing test1
        {"test2"}, //test description: testing test2
    };
  }

  @Override
  public void run(IHookCallBack callBack, ITestResult testResult) {
    Object[] parameters = testResult.getParameters();
    callBack.runTestMethod(testResult);
    if (parameters.length != 0) {
      String testname = parameters[0].toString();
      testResult.setTestName("Dragon_Warrior" + testname);
    }
  }

  @Override
  public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites,
      String outputDirectory) {
    suites.stream()
        .flatMap(iSuite -> iSuite.getResults().values().stream())
        .flatMap(iSuiteResult -> iSuiteResult.getTestContext().getPassedTests().getAllResults().stream())
        .forEach(iTestResult -> {
          System.err.println("Name : " + iTestResult.getName());
        });
  }
}