0

I am using Data Provider in and report is displaying the same method name in all the tests in TestNG report.

static String TC_Name;
@DataProvider(name="SampleTestData")
public Object[][] testData() {
    return new Object[][]{
        {"TestCase1","Description1",""},
        {"TestCase2","Description2",""},
        {"TestCase3","Description3",""},
        //{"TestCaseID_2","TC_Description","Negative",""},
    };
}


@Test(dataProvider="SampleTestData", alwaysRun = true,testName="SampleTest")
public void testCaseInitialization(String testcaseID,String testcaseName, String Temp) {
    Log.startTestCase(testcaseID + "_" + testcaseName);
    TC_Name= testcaseID;
}

Now the output i want I want is "TestCase(1-3)". But i am getting "testCaseInitialization" in report.

I have tried the below way of hacking the report.:

@AfterMethod(alwaysRun = true)
public void setResultTestName(ITestResult result) {
    try {
        BaseTestMethod baseTestMethod = (BaseTestMethod) result.getMethod();
        Field f = baseTestMethod.getClass().getSuperclass().getDeclaredField("m_methodName");
        f.setAccessible(true);
        f.set(baseTestMethod, TC_Name);
    } catch (Exception e) {
       // ErrorMessageHelper.getInstance().setErrorMessage(e);
        Reporter.log("Exception : " + e.getMessage());
    }
}

But this is giving me the "TestCase3" in all method names.

Can please anybody suggest , how else can I hack the report.

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
Aneesh Goel
  • 47
  • 2
  • 6
  • Have you tried two links below? [Retrieve test name on TestNG](https://stackoverflow.com/questions/8596632/retrieve-test-name-on-testng?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) [Custom test method name in TestNG reports](https://stackoverflow.com/questions/15220262/custom-test-method-name-in-testng-reports?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) Link to the TestNG documentation in first post: http://testng.org/doc/documentation-main.html#invokedmethodlistener. – Rub Jun 15 '18 at 05:59
  • Yeah , i have followed the above link only, but still couldn't able to get it Right. I am getting the last testCase name in all the methods in Report – Aneesh Goel Jun 16 '18 at 10:13

1 Answers1

0

You can customize method name with @Test annotation method only, if you want to retrieve "TestCase(1-3)" then apply it to Test Method. Report invoke method name of @Test method.

Ishita Shah
  • 3,955
  • 2
  • 27
  • 51