Re: [testng-users] Is it possible to display custom methods name in TestNG reports?

4,958 views
Skip to first unread message

Cédric Beust ♔

unread,
Aug 10, 2012, 9:18:29 AM8/10/12
to testng...@googlegroups.com
Have your test class extend org.testng.ITest and override getTestName().

-- 
Cédric




On Fri, Aug 10, 2012 at 5:49 AM, Arun Panda <arun.k...@gmail.com> wrote:
I know this can be done for junit reports generated by testng by extending JUnitReportReporter and overriding getTestName method.
But can this be done for TestNG reports? I really want this feature so that testcases having dataprovider can be displayed with some custom name e.g
methodname_filename for testng reports

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/lfiUoJB7FRAJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.


Arun_testng

unread,
Aug 13, 2012, 5:52:12 AM8/13/12
to testng...@googlegroups.com
Thanks!!

But this turns out to be much more difficult to implement than i thought.
It was more straight forward while using it with JunitReportReporter listner, were i was able to get current method under exceution quite easily



    @Override
    protected String getTestName(ITestResult result)
    {
       if(result.getParameters()!=null )
       {
            TestParameters params = (TestParameters) result.getParameters()[0];
            HashMap<String, String> map = params.getParameters();
            String filepath = null;
            if(map.containsKey("INPUT_FILE"))
            {
                filepath = map.get("INPUT_FILE");
                StringBuilder ret = new StringBuilder();
                ret.append(result.getMethod().getMethodName());
....


Now how do i get method name in this case (i.e extending each test case or the base test case with ITest class.)

 My second query is, I am using jenkins to display testng results where i provide "test-output\*.xml" as TestNG XML report pattern, so
if i succeed to change the method name will it be updated in testng result xml also?

Thanks,
Arun





On Fri, Aug 10, 2012 at 6:49 PM, Cédric Beust ♔-2 [via TestNG] <[hidden email]> wrote:
Have your test class extend org.testng.ITest and override getTestName().

-- 
Cédric




On Fri, Aug 10, 2012 at 5:49 AM, Arun Panda <[hidden email]> wrote:
I know this can be done for junit reports generated by testng by extending JUnitReportReporter and overriding getTestName method.
But can this be done for TestNG reports? I really want this feature so that testcases having dataprovider can be displayed with some custom name e.g
methodname_filename for testng reports

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/lfiUoJB7FRAJ.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



If you reply to this email, your message will be added to the discussion below:
http://testng.1065351.n5.nabble.com/Is-it-possible-to-display-custom-methods-name-in-TestNG-reports-tp16315p16317.html
To unsubscribe from TestNG, click here.
NAML



View this message in context: Re: Is it possible to display custom methods name in TestNG reports?
Sent from the testng-users mailing list archive at Nabble.com.

Krishnan Mahadevan

unread,
Sep 7, 2012, 4:15:55 AM9/7/12
to testng...@googlegroups.com
No, this cannot be done with the DataProvider. You will have to resort to using Factory annotation.
Here's how you would do it :

Create a @Factory annotated method and have it create instances of your test classes. The testclass can conveniently implement ITest interface. It would be good if you had your test class such that it has just one method name, there by helping you achieve this.

If this is also not a viable option, then you may have to resort to writing your own implementation of IReporter 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Wed, Aug 29, 2012 at 12:19 PM, Neeraja <neeraja.r...@gmail.com> wrote:


Hi,
I am also facing a similar issue.
I need to overwrite the testcase name when i use Dataprovider for passing test data. Currently all tests show the same name in the report. I tried implementing ITest but don't know how i can overwrite the getTestName() method so that i can get different testcase name for each of the dataset. Can some one give me sample code with end to end integration. 

 Is there  a way where i pass my own test case name in the dataprovider as an argument which will be shown in the report.(This way i will get different name for each dataset)

Thanks,
Neeraja.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/AxtxtDWf9o0J.

KV

unread,
Jul 29, 2013, 12:50:21 PM7/29/13
to testng...@googlegroups.com
Hi Krishnan,

Could you please provide a sample code that can help achieve this? I used Factory with dataprovider but it gets executed before the test methods so I am not able to get the test method names which would make the name unique.
This is the code I have so far,

public class NewTest implements ITest {

String dataname = null;

@DataProvider(name="BasicDataProvider")

    public Object[][] getTestData() {

        Object[][] data = new Object[][] {

                { new TestParameters("TestCase1", "Sample test 1")},

                { new TestParameters("TestCase2", "Sample test 2")},

                { new TestParameters("TestCase3", "Sample test 3")},

                { new TestParameters("TestCase4", "Sample test 4")},

                { new TestParameters("TestCase5", "Sample test 5") }

        };

        return data;

    }

public NewTest() {}

@Factory(dataProvider = "BasicDataProvider")

public NewTest(TestParameters testParams) {

String testCase = "";

String classname;


if (testParams != null) {

testCase = testParams.getTestName();

}

// dataname = String.format("%s(%s)", cake);


Class<?> enclosingClass = getClass().getEnclosingClass();

if (enclosingClass != null) {

classname = enclosingClass.getName();

} else {

classname = getClass().getName();

}

dataname = classname + " " + testCase;

// System.out.println(">>>>>>><<<<<<<<<<<<<<"+dataname);

System.out.println("factory");


}


@Test(dataProvider="BasicDataProvider")

    public void testSample1(TestParameters testParams){

System.out.println("The test is run");

        //test code here

    }


    @Test(dataProvider="BasicDataProvider")

    public void testSample2(TestParameters testParams){

        //test code here

    }



@Override

public String getTestName()

{

return dataname;

}

}


It now just gets testsample 1 to 5. It would be unique if it can get the test method name along with it which would make it unique.


Thanks

KV.

To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

Krishnan Mahadevan

unread,
Jul 31, 2013, 6:39:42 AM7/31/13
to testng...@googlegroups.com
Karthik,

Can you please take a look at this post of mine and see if that helps get started ?



Thanks and Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

p...@groupon.com

unread,
Oct 16, 2013, 5:37:34 PM10/16/13
to testng...@googlegroups.com
Thank you so much. Your answer helped me after 4 months :)

Milen Georgiev

unread,
Feb 21, 2017, 4:51:57 AM2/21/17
to testng-users, p...@groupon.com
I have excel-data-driven @Test methods and need to see them multiple times post-fixed by rowNum /feed, i.e.

positiveProfileEditTest() has three testCases /executions:


-positiveProfileEditTest_1

-positiveProfileEditTest_2


-positiveProfileEditTest_3






Jessie

unread,
Feb 21, 2017, 10:37:16 AM2/21/17
to testng...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

Milen Georgiev

unread,
Mar 22, 2017, 10:16:54 AM3/22/17
to testng-users, ced...@beust.com
Thank you Yanan, but in your link /example, just like in other implementations shown above, the test-instance-name attribute is filled with the customTestCaseMethodInstance name. I need the "name" attribute of <test-method> to be filled with the custom name. This is used by TestResultsAnalyzer jenkins plugin I use. I mean, this plugin can feed from testng-results.xml and it gets exactly the name attribute.

Milen Georgiev

unread,
Mar 22, 2017, 10:21:58 AM3/22/17
to testng-users, ced...@beust.com

The following looks better for my case

"http://stackoverflow.com/questions/20887821/how-can-i-override-the-test-method-name-that-appears-on-the-testng-report
  writing a TestListenerAdapter that somehow overrides the ITestResult.getTestNameMethod() method?"

rather than

Milen Georgiev

unread,
Mar 22, 2017, 10:31:16 AM3/22/17
to testng-users, ced...@beust.com

But I still haven't found an implementation of "somehow overrides the ITestResult.getTestNameMethod() method" and I am not sure, if this is possible.

Another solution proposal could be an implementation of a custom reporter based on the TestNG test-results.xml reporter:

https://www.tutorialspoint.com/testng/testng_custom_reporter.htm
https://www.tutorialspoint.com/testng/testng_test_results.htm
https://www.tutorialspoint.com/testng/testng_html_xml_report.htm

But here I need to use /inherit somehow the features of "TestNG test-results.xml reporter" and producing, for example test-custom-results.xml. Thus, as I would like to have only one difference, compared to test-results.xml, i.e. to have custom method names here <test-method name="XXX">
Reply all
Reply to author
Forward
0 new messages