I am working on a project where I need to invoke TestNG programatically(using data providers). Things are fine except that in the report, we are getting the name of the @Tes
If you want to change the name in the HTML report, it'll be a total hack. Here's how I did it:
public class NinjaTest {
...
...
@AfterMethod (alwaysRun = true)
public void afterMethod(ITestResult result, Method method) {
try {
//I have XML test suites organized in directories.
String xmlFile = result.getTestContext().getCurrentXmlTest().getSuite().getFileName();
String suiteName = xmlFile.substring(xmlFile.lastIndexOf("\\") + 1, xmlFile.lastIndexOf(".xml"));
String pathToFile = xmlFile.substring(0, xmlFile.lastIndexOf("\\") );
String directory = pathToFile.substring(pathToFile.lastIndexOf("\\") + 1);
String testMethodName = String.format("%s/%s - %s", directory, suiteName, method.getName());
//Total hack to change display name in HTML report \(^o^)/
Field methodName = org.testng.internal.BaseTestMethod.class.getDeclaredField("m_methodName");
methodName.setAccessible(true);
methodName.set(result.getMethod(), testMethodName);
} catch (Exception e) {
// Eh.... ¯\_(ツ)_/¯
e.printStackTrace();
}
}
...
...