How can I override the test method name that appears on the TestNG report?

Viewed 3747

How can I override the test name that appears on the TestNG report? I want to override the name that appears in the middle column (currently shows as the method name). Is this even possible?

enter image description here

I tried to do it like this, but it didn't work.

public class EchApiTest1 extends TestBase {
  ...
@BeforeTest
public void setUp() {
    restClient = new RestClientPost();
    this.setTestName( "ech: XXXXXX" );
}

And, the base class:

import org.testng.ITest;    
public class TestBase implements ITest {        
    String testName = "";    
    @Override
    public String getTestName() {
        return this.testName;
    }           
    public void setTestName( String name ) {
        this.testName = name;
    }    
}

NOTE: The above code does work when I am viewing the report detail in the Jenkins TestNG plugin report, which shows the overridden test name as a string called "Instance Name:" at the beginning of the Reporter log output. Why, in this case, WHY does a "setTestName()" method alter a string labeled "Instance Name" in the report?

One answer I found had a suggestion like this but I don't know how to pass an ITestResult arg to a AfterMethod method:

@AfterMethod
public void setResultTestName( ITestResult result ) {
  try {
    BaseTestMethod bm = (BaseTestMethod)result.getMethod();
    Field f = bm.getClass().getSuperclass().getDeclaredField("m_methodName");
    f.setAccessible(true);
    f.set( bm, bm.getMethodName() + "." + your_customized_name );        
  } catch ( Exception ex ) {
  Reporter.log( "ex" + ex.getMessage() );
 }

Thoughts?

3 Answers
Related