Data Provider -How to either change run time testName for each run or call @Test methods based on @Test(data provider) received value.

199 views
Skip to first unread message

QA Lead

unread,
Jan 31, 2014, 1:07:29 AM1/31/14
to testng...@googlegroups.com
Hi,



Using @DataProvider(name="runMode"), I am able get runMode and then using @DataProvider(name=testCases") I am able to read each tests from one or more sheets along with corresponding value from data sheet (used @DataProvider(name="runMode") data-provider). But my @Test(Data-Provider="testCases") gives same name for each run. Let us say it run 50 times then it will show like below

ReadSuite.testDataPr
ovi(java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String)

for each run. I want it should give 50 names based on keyword.

Krishnan Mahadevan

unread,
Jan 31, 2014, 3:41:25 AM1/31/14
to testng...@googlegroups.com
You can refer to my blog post : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/ and see if that helps you get started. 
--
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...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/

QA Lead

unread,
Feb 1, 2014, 12:26:02 AM2/1/14
to testng...@googlegroups.com
Hi Krishnan,

Thanks for reply but it seems your blog is not going to provide solution for this scenario. Please have a look at my actual sample output
PASSED TESTS -null
Class Method Time
(ms)
Status
[TestClass name=class selenium.bu.ReadSuite]   ReadSuite.testDataProvi(java.lang.String,
                                                                         java.lang.String, java.lang.String, java.lang.String,
                                                                         java.lang.String, java.lang.String,java.lang.String)                
                                                                         [pri:0,instance:selenium.bu.ReadSuite@24dfb3be]
                                           3147 PASSED
[TestClass name=class selenium.bu.ReadSuite]    ReadSuite.testDataProvi(java.lang.String,
                                                                         java.lang.String, java.lang.String, java.lang.String,
                                                                         java.lang.String, java.lang.String,java.lang.String)                
                                                                         [pri:0,instance:selenium.bu.ReadSuite@24dfb3be]
                                               154 PASSED
[TestClass name=class selenium.bu.ReadSuite]   ReadSuite.testDataProvi(java.lang.String,
                                                                         java.lang.String, java.lang.String, java.lang.String,
                                                                         java.lang.String, java.lang.String,java.lang.String)                
                                                                         [pri:0,instance:selenium.bu.ReadSuite@24dfb3be]
                                                  79 PASSED
[TestClass name=class selenium.bu.ReadSuite]   ReadSuite.testDataProvi(java.lang.String,
                                                                         java.lang.String, java.lang.String, java.lang.String,
                                                                         java.lang.String, java.lang.String,java.lang.String)                
                                                                         [pri:0,instance:selenium.bu.ReadSuite@24dfb3be]
                                                      1 PASSED

where in actual I want, report should look like
[TestClass name=class selenium.bu.ReadSuite]      ReadSuite.browse()[pri:0,instance:selenium.bu.ReadSuite@24dfb3be]           3147 PASSED
[TestClass name=class selenium.bu.ReadSuite]      ReadSuite.checkToolTip()[pri:0,instance:selenium.bu.ReadSuite@24dfb3be]    154 PASSED
[TestClass name=class selenium.bu.ReadSuite]      ReadSuite.clear()[pri:0,instance:selenium.bu.ReadSuite@24dfb3be]                 79 PASSED
[TestClass name=class selenium.bu.ReadSuite]      ReadSuite.clearAll()[pri:0,instance:selenium.bu.ReadSuite@24dfb3be]               1 PASSED


On Friday, January 31, 2014 2:11:25 PM UTC+5:30, Krishnan wrote:
You can refer to my blog post : http://rationaleemotions.wordpress.com/2013/07/31/pretty-printing-with-testng/ and see if that helps you get started. 

On Friday, January 31, 2014, QA Lead <sanjee...@gmail.com> wrote:
Hi,



Using @DataProvider(name="runMode"), I am able get runMode and then using @DataProvider(name=testCases") I am able to read each tests from one or more sheets along with corresponding value from data sheet (used @DataProvider(name="runMode") data-provider). But my @Test(Data-Provider="testCases") gives same name for each run. Let us say it run 50 times then it will show like below

ReadSuite.testDataPr
ovi(java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String)

for each run. I want it should give 50 names based on keyword.

--
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.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

Krishnan Mahadevan

unread,
Feb 3, 2014, 1:50:55 AM2/3/14
to testng...@googlegroups.com
Sanjeev
Please have your DataProvider accept a POJO [ Plain Old Java Object] as a parameter and have it implement a custom toString() method which gives out the output as you need.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

QA Lead

unread,
Feb 3, 2014, 4:53:05 AM2/3/14
to testng...@googlegroups.com
Hi Krishnan,

  Thanks for the solution which you have mentioned.
  Can you provide me little help about POJO as I never used this one.

Alexander Poulikakos

unread,
Feb 3, 2014, 4:58:46 AM2/3/14
to testng...@googlegroups.com

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.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.



--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.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...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

QA Lead

unread,
Feb 3, 2014, 10:00:42 AM2/3/14
to testng...@googlegroups.com
Hi Alex,

 Even this wiki does not contain much info about POJO.

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



--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.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...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

--
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...@googlegroups.com.

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

Alexander Poulikakos

unread,
Feb 3, 2014, 1:53:25 PM2/3/14
to testng...@googlegroups.com

Hi

 

A POJO (Plain Old Java Object) is just a simple java class. For example this is a POJO:

 

public class MyPojo {

    private String a;

 

    public MyPojo(String a) {

        this.a = a;

    }

    public String getA() {

        return a;

    }

    @Override

    public String toString() {

        return a;

    }

}

 

 

You can have your DataProvider return POJO’s, and your @Test methods can take POJO’s as arguments.

Giannis Papadakis

unread,
Feb 4, 2014, 3:27:56 AM2/4/14
to testng...@googlegroups.com
You can use reflection to set the name of the method in results.

public void SetResultTestName(String methodName) {

try 
{
ITestResult result=Reporter.getCurrentTestResult();
BaseTestMethod bm = (BaseTestMethod)result.getMethod();
Field f = bm.getClass().getSuperclass().getDeclaredField("m_methodName");
f.setAccessible(true);
f.set(bm,methodName);        

}
catch (Exception ex) {
System.out.println("ex" + ex.getMessage());
}}

Giannis


2014-02-03 Alexander Poulikakos <alexander....@ericsson.com>:

QA Lead

unread,
Feb 4, 2014, 8:06:17 AM2/4/14
to testng...@googlegroups.com
Hi Alex,

Thanks for the solution but I am facing problem when used POJO, have created POJO class as well
@DataProvider(name = "runTest")
    public TestProvider myTest() throws BiffException, IOException, Exception
    {  
    //code was there file reading ...
        testsWorkbook = Workbook.getWorkbook(file);
        Object[][] testCaseList=null;
        int totalRows=0;
        TestProvider testp = new TestProvider();// Creating POJO object
    int i, j;
    i=0;
        for(int row=0;row<10;row++) //Assuming maximum row is 10
        {
       
            j=0;
 
            for (int col = 1; col < 5; ) //Assuming maximum column is 5
            {
               
        testCaseList[i][j]=tab[col][row]

                        // Initializing POJO
                testp.setTest_Step_id((String) testCaseList[i][j++]);
                testp.setTest_step((String) testCaseList[i][j++]);
                testp.setObject_val((String) testCaseList[i][j++]);
                testp.setDsClm_name((String) testCaseList[i][j++]);
                testp.setAssert_val((String) testCaseList[i][j++]);
                testp.setDescription((String) testCaseList[i][j++]);
                testp.setAsrt_val_ds((String) testCaseList[i][j++]);

                //testCaseList[i][j]=testp;
                        // end of POJO initialization
            }
        }  
        //return testCaseList;
        return testp;
    }

 public void testDataProvi(String s1,String s2,String s3,String s4,String s5,String s6,String s7) throws Exception
    {
      now I want logic like
      if s2.equals("click")
           testClick() //calling test function
      .....
    }

testClick()
{
  ....
}
 when it will run, it should say in report that ReadSuite.testClick()  PASSED
and so on for others ...

QA Lead

unread,
Feb 4, 2014, 9:16:19 AM2/4/14
to testng...@googlegroups.com

Hi Giannis,

Thanks for your answer !
Should I call this reflection method in listner which I have created or how to call it ?

Giannis Papadakis

unread,
Feb 4, 2014, 9:40:56 AM2/4/14
to testng...@googlegroups.com
You can call it inside the listener or inside your dataProvider. It always returns the currentTestResult from Reporter class which is thread local variable (for multiple threads.)


2014-02-04 QA Lead <sanjee...@gmail.com>:

QA Lead

unread,
Feb 4, 2014, 11:59:37 PM2/4/14
to testng...@googlegroups.com
Hi Giannis,

Thanks for the answer. I called it from @Test (DataProvider="xyz") but getting same result and expected result is not coming. I am expecting result like

[TestClass name=class selenium.bu.ReadSuite]   ReadSuite.testDataProvi(java.lang.String,
                                                                         java.lang.String, java.lang.String, java.lang.String,
                                                                         java.lang.String, java.lang.String,java.lang.String)                
                                                                         [pri:0,instance:selenium.bu.ReadSuite@24dfb3be]
           

where in actual I want, report should look like
[TestClass name=class selenium.bu.ReadSuite]      ReadSuite.browse()[pri:0,instance:selenium.bu.ReadSuite@24dfb3be]           

QA Lead

unread,
Feb 5, 2014, 1:34:32 AM2/5/14
to testng...@googlegroups.com
Hi,
Thanks for providing sample POJO.
I implemented it but @DataProvider(name = "runTest")    public Object[][] myTest() {} normally return Object type variable and when I am trying to return as POJO variable, it prompts return type error.


On Tuesday, February 4, 2014 12:23:25 AM UTC+5:30, drapostolos wrote:

QA Lead

unread,
Feb 5, 2014, 9:10:41 AM2/5/14
to testng...@googlegroups.com
Hi Giannis,,

I am still facing problem in reporting just because of testName issue,

Giannis Papadakis

unread,
Feb 5, 2014, 9:33:54 AM2/5/14
to testng...@googlegroups.com
You can call the method inside your @Test and feed it from dataprovider for the name of the test in report.


2014-02-05 QA Lead <sanjee...@gmail.com>:

QA Lead

unread,
Feb 5, 2014, 9:55:32 AM2/5/14
to testng...@googlegroups.com
Hi John,
I already called it from @Test(DataProvider="xyz") and do not understand about feeding. Did you suggest feeding the function "SetResultTestName" ?

public void testDataProvi(String s1,String s2,String s3,String s4,String s5,String s6,String s7) throws Exception
    {  
        SetResultTestName(s2);
         .....

     }
public void SetResultTestName(String methodName)
    {
        try
        {
        ITestResult myResult=Reporter.getCurrentTestResult();
        BaseTestMethod bm = (BaseTestMethod)myResult.getMethod();

QA Lead

unread,
Feb 6, 2014, 3:02:58 AM2/6/14
to testng...@googlegroups.com
Does we haney solution regarding this issue ?

QA Lead

unread,
Feb 6, 2014, 4:11:44 AM2/6/14
to testng...@googlegroups.com
Hi John,

  Sorry for typo error :(
   do we have any other remarks or solution ?

QA Lead

unread,
Feb 7, 2014, 8:03:57 AM2/7/14
to testng...@googlegroups.com
Hi
Any new update on it ?


On Wednesday, February 5, 2014 10:29:37 AM UTC+5:30, QA Lead wrote:

Yanan

unread,
Jan 20, 2017, 5:01:35 PM1/20/17
to testng-users
Have you got the answer? If so, can you please share?
Thanks!
Reply all
Reply to author
Forward
0 new messages