0

I am trying to override the test case name in the testNg HTML report since I am using the data provider and each test data depicts something different. To do this, I added the following:

I am setting the test name using the data provider. My data provider has the relevant test case name.

Here is what I am doing :

   @BeforeMethod(alwaysRun = true)
   public void setTestName(Method method, Object[] row, ITestContext context) {
     testName.set(row[0].toString()); // row ["<testCaseName1", "..", "....">]
     context.setAttribute("testName", row[0].toString()); // row ["<testCaseName1", "..", "....">]
    }

I have tried setting up in the test context too. I see that on console the test case names seem to come different and the correct ones which I am passing in the parameter.

However, in the default testNG HTML report, the test names are not getting changed. If the data provider has 4 different test data, then a particular test which runs 4 times with different test data, is being shown in the report with the same name.

0

You should use ITestResult to change the name of test in a report. For example this is the complete solution:

Your dependencies:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>reportng</artifactId>
        <version>1.2.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Your test class:

package click.webelement;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class CustomReportName {

    @DataProvider(name = "db")
    public Object[][] createData() {
        return new Object[][] {{"test 1"}, {"test 2"}};
    }

    @Test(dataProvider = "db")
    public void testParameterized(String msg, ITestContext context){
        context.setAttribute("customTestName", "Custom: " + msg);
    }

    @Test
    public void noDataProvider(){

    }

    @AfterMethod()
    public void setTestNameForReport(ITestResult testResult, ITestContext context){
        if(testResult.getMethod().getDataProviderMethod() != null){
            testResult.setTestName(context.getAttribute("customTestName").toString());
        }else{
            System.out.println("No data provider for this method");
        }
    }

}
3
  • This works, however, there are few tests which actually don't have a data provider, in such cases the last attributes get overriden. Is there a way to clear these attributes? I tried removing the added attribute in afterTest, but that doesn't work.
    – Mahima
    May 6 at 8:26
  • Test attribute for null. See my update. If it is null then it was not set for the test hence no customization applied.
    – Alexey R.
    May 6 at 8:38
  • @Mahima wrong. The proper way would be to check if a method has data provider assigned. Fixed and updated my example.
    – Alexey R.
    May 6 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.