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.

Canberk Akduygu