How to Change a Test Name When Using TestNG DataProvider

Hello everybody, One of the great features of TestNG is TestNG DataProvider. TestNG DataProvider simply provides a way to run one test with different data with multiplying the test code. General usage is like below:

Whenever you run that test, you will have a report with 6 execution results and the name of the test are always the same cause we execute. You will only see the parameters that have been sent to the test function. But in case you implement a reporting mechanize, you never see those parameters because testNG get the test name during execution. You will get lost in the results.

TestNG provides us a way to change the test name during execution. Then how we implement it?

First, create a Thread Local String object to store your test case name.

Secondly, implement a BeforeMethod function to override your test name before the execution.

Third, you need to implement ITest interface into your test. After implementing it, it will tell you to override getName() method. This where you get the test name, so we need to modify this one.

Finally, run your test, you will realize that the test name is fetched by our overridden getTestName() method.

Sample Source Code

But this change will only affect TestNG’s built-in reports. In case you are using 3rd party library like ExtentReports to generate test execution reports. You need to do one more thing to show this modified name in your reports.

Pass a ITestContext object to your @BeforeMethod and use setAttribute to set modified name in to the context object. You should make an if-else condition incase the name is not changed because some tests don’t use DataProvider so the name is not changed.

Then in your reporting class, you need to get the modified name via iTestResult object by using below usage.

Thanks.
Canberk Akduygu

By |2018-12-04T22:32:39+00:00September 1st, 2018|TestNG Tutorials|12 Comments

About the Author:

Canberk Akduygu is as Test Architect working on Test Automation and Performance Testing.

12 Comments

  1. Ajith November 23, 2018 at 1:32 am - Reply

    Hi, Could you please explain how did you create the reporting class?

  2. Canberk Akduygu November 23, 2018 at 11:48 am - Reply

    Actually, it was an example from my real life project. You may go to http://www.swtestacademy.com/extentreports-testng/ to understand how you can do some reporting in your project.

  3. Raman November 28, 2018 at 9:29 am - Reply

    Hi,
    Thank you for the explanation. But while using above example I am getting the same test-instance-name=”PMO” for all the test-method in testng-results.xml. How I will be able to get the test-method name as the name of my dataprovider value in the testng-results.xml

  4. Raman November 28, 2018 at 9:48 am - Reply

    Hi,
    Thank you for the explanation, but while using the above example I am getting the same value in test-instance-name of test-method in testng-results.xml file for all the different value parameter of data-provider.

    What I am expecting is the test-method should display name or test-instance-name differently so that test failure can be identified easily.
    Thanks in advance.

  5. Canberk Akduygu November 28, 2018 at 11:30 am - Reply

    I should definitely see a sample project. The one I describe above is a real life project and it’s working fine. May be you can create a simple project in github and we can solve the problem.

  6. Raman November 28, 2018 at 11:41 am - Reply

    I will try to do that but my question is even if I try to run the source code provided by you above testng-results.xml file is generating same test-instance-name i.e. test-instance-name=”PMO”(which the last object of data provider) and same name i.e. name=”createUser” for the the class test-method parameters.

  7. Canberk Akduygu November 28, 2018 at 2:15 pm - Reply

    I am going to check it.

  8. Raman November 28, 2018 at 2:46 pm - Reply

    Thank you for your reply. I have been taking help from different blogs and StackOverflow but unsuccessful. Please help me to find the solution. This is something which I tried.

    public class DataProviderChangeName implements ITest {
    private static ThreadLocal testNames = new ThreadLocal(){
    @Override
    protected String initialValue() {
    return “”;
    }
    };

    @Test (dataProvider = “dp”)
    public void testMethod(int number, String text) {
    String txt = “Thread Id [” + Thread.currentThread().getId() + “] with value (” + number + “,” + text + “)”;
    testNames.set(txt);
    }

    @DataProvider (name = “dp”)
    public static Object[][] getData() {
    return new Object[][] {
    {1, “Poetry”},
    {2, “Novels”},
    {3, “TextBooks”}
    };
    }

    @Override
    public String getTestName() {
    return testNames.get();
    }
    }

    Looking forward for your reply.
    Thanks in advance

  9. Raman November 29, 2018 at 1:20 pm - Reply

    Hi,
    Did you get a chance to find a solution?
    Thanks..

  10. Canberk Akduygu November 29, 2018 at 1:40 pm - Reply

    Actually, whenever I trigger the tests via suite.xml names are changing in html report but I have problem with gradle test command. That might be an issue with TestNG version. We should investigate on TestNG github page

  11. Raman November 29, 2018 at 2:25 pm - Reply

    Thank you for an update @Canberk Akduygu. I will verify on TestNG github page.

  12. Raman November 29, 2018 at 2:30 pm - Reply

    In case you come across any solution, please do update. ūüôā

Leave A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.