Parameters & Dataprovider to Test method

3,301 views
Skip to first unread message

Pavithra

unread,
Nov 24, 2011, 5:43:24 AM11/24/11
to testng-users, cbe...@gmail.com
In TestNG, is it possible to use both @Parameters & @Dataprovider
annotation.
I mean when I use both, how can I access the parameters for my test
method.

E.g: I have a simple parameter username which i want to get using
@Parameters annotation.
All other parameters e.g a,b,c,d I want to take from data provider.

I created data provider named "Fetch Data" in my framework.
and Test method as below:

@Parameters("username")
@Test (groups={"Smoke","Contacts"}, dataProvider = "Fetch Data")
public void test_method1(String username, String a, String b, String
c, String d) throws Exception {
bla bla bla ;
}

The above method gives error message saying

org.testng.TestNGException:
Number of parameter values passed in using data provider is less than
number of parameters defined on test method and TestNG is unable in
inject a suitable object

Hence, I wanted to know how to differentiate that username is the
parameter coming from @Parameters annotation ?

Thanks,
Pavithra

Krishnan Mahadevan

unread,
Nov 24, 2011, 7:32:33 AM11/24/11
to testng...@googlegroups.com
I dont think you can do that.
You cannot couple @Parameters and @DataProvider annotation on a @Test Method.

Parameters and DataProvider annotation were introduced into TestNG with the sole purpose of parameterizing tests.
They are provided as 2 options to parameterize your tests, but not meant to be used in conjunction with each other.

read  under: 

5.5 - Parameters

Why would you want to couple them ?

If you are looking at having one userName being used with multiple values from a dataprovider then you should enhance your dataprovider such that it returns the same userName for various other data combinations for every iteration.


Thanks & Regards
Krishnan Mahadevan

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


Pavithra

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
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.


Simon Knott

unread,
Nov 24, 2011, 7:44:01 AM11/24/11
to testng...@googlegroups.com
I must admit that I've wanted to combine them before in the past and faced similar frustrations.

Sometimes you have data which you want to pass in which needs to be accessible for the whole test suite (environment URLs, etc), which is when I've used parameters, and sometimes you need to run the same tests with multiple datasets, with data providers.  Occasionally, a test method will require access to both sets of data!

The only way I've found to get around this is to pull the parameters out in a beforeXXX method and throw them into the ITestContext.

Krishnan Mahadevan

unread,
Nov 24, 2011, 7:47:30 AM11/24/11
to testng...@googlegroups.com
Simon,
DataProviders can themselves be parameterized.
Take a look at this : http://www.lysergicjava.com/?p=165
This Link is also available under http://testng.org/doc/misc.html
with the topic name as "Passing parameters to @DataProviders."

Would that help ?

Thanks & Regards
Krishnan Mahadevan

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



--
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/-/s1W9FPe3KtAJ.

P.Hill

unread,
Dec 1, 2011, 8:17:28 PM12/1/11
to testng...@googlegroups.com
On 11/24/2011 4:47 AM, Krishnan Mahadevan wrote:
>
> DataProviders can themselves be parameterized.
>
And then the arguments provided by the DataProvider in the
Iterator<Object[]>, can be any combination of arguments for the test
taken from parameters, member variables, something read from somewhere a
file, database etc.
It is actually very powerful.

-Paul

somesh bansal

unread,
Sep 27, 2012, 6:43:05 AM9/27/12
to testng...@googlegroups.com, cbe...@gmail.com

Hello All,

I am facing an issue with @parameters & @dataproviders

My requirement:

Want to pass dynamic dataProvider name to the test-method (A)

      1> XML file has 2 <test> tags
                  a> runAll b> runCritical
      2> When I execute, 'runAll' --> test-method (A) should take dataprovider name =dp1
           When I execute, 'runCritical' --> test-method (A) should take dataprovider name =dp2

   "dp1" fetches values executing other method (B) and then passes to testMethod(A)
   "dp2" gets value from XML i.e. passed along in <test as parameters>  using (ITestContext context) and passes to testMethod(A)

 Request to let me know your valuable comments

 Awaiting response
 
 Regards
 Somesh

Roman Hiden

unread,
Sep 27, 2012, 11:47:26 AM9/27/12
to testng...@googlegroups.com, cbe...@gmail.com
I do a parameter @BeforeTest
    @BeforeTest
    @Parameters({ "FilePath" })
    public void Before(String filePath) {
this.filePath=filePath;
}
then
@DataProvider(name = "excelProvider", parallel = true)
    public Iterator<Object[]> getAccounts() {
//initialize dataprovider using the filePath in @BeforeTest

Somesh Bansal

unread,
Sep 28, 2012, 1:28:37 AM9/28/12
to testng...@googlegroups.com
Hi Roman,

I tried your approach but unable to refer 'filePath' to @Test (dataProvider = filePath) as it says Variable attribute must be a constant. filePath is declared as String

Bit, confused

Awaiting response

Somesh

--
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/-/Awkwb-nrArUJ.

Krishnan Mahadevan

unread,
Sep 28, 2012, 4:25:38 AM9/28/12
to testng...@googlegroups.com
Somesh,
The approach suggested by Roman, was not aimed at changing the dataProvider attribute value [which is actually equal to dynamically changing the data provider itself] but was more aimed at having the same data provider provide you with different sets of data based on the parameters being passed in via @Parameters annotation in the @BeforeTest annotation.

so your dataprovider would just be the same single method, but it would now produce different sets of data based on conditions.
It would test the value that is being passed via the @Parameters annotation and then based on its value, it would do the needful.

Hope that clears out the confusion.


Thanks & Regards
Krishnan Mahadevan

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




Somesh Bansal

unread,
Sep 28, 2012, 6:03:47 AM9/28/12
to testng...@googlegroups.com
Hello Krishnan,

Thanks for your help!!

I hope that it might resolve my problem. Will try & let you know
In addition to it, got one problem too. I am executing script from ant as follows

<target name="runTests" description="Running tests" >
        <echo>Running Tests...</echo>
        <taskdef resource="testngtasks" classpathref="libs"/>
        <ac:for list="${antPrgrm}" param = "program">
            <sequential>
                <echo>Program: @{program}</echo>
                <ac:for list="${antBrwsr}" param = "drvr">
                    <sequential>
                        <echo>Driver: @{drvr}</echo>
                        <testng outputDir="${report.dir}\@{program}_@{drvr}" classpathref="libs" delegatecommandsystemproperties="true">  
                            <xmlfileset dir="${basedir}" includes="program.xml"/>
                            <sysproperty key="program" value="@{program}AccountProperty"/>   
                            <sysproperty key="drvr" value="@{drvr}"/>
                            <sysproperty key="os" value="${antOS}"/>
                        </testng>
                    </sequential>
                </ac:for> 
            </sequential>
        </ac:for>         
        
Here, I am passing different system properties along and able to get value at class level.

But, unable to get the value of 'sysproperty' in the program.xml. Can you let me know how can I access the same

Regards
Somesh

Somesh Bansal

unread,
Sep 28, 2012, 6:09:45 AM9/28/12
to testng...@googlegroups.com
As I want to give suite name dynamically (as an eg:)

Program.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="<WantSysProgramReferenceHere>">
    <test name="Staging-SmokeTest" preserve-order="true" parallel="false">
        <parameter name="tstNm" value="SmokeTest"/>
        <classes>
            <class name="ABC"/>
            <class name="DEF"/>           
        </classes>
    </test>    
</suite>

Let me know incase of any more information is needed

Thanks in advance!!
Somesh

Krishnan Mahadevan

unread,
Sep 28, 2012, 9:52:46 AM9/28/12
to testng...@googlegroups.com
I dont know if you can access VM arguments (fetched via System.getProperty()) within the TestNG xml file.

Thanks & Regards
Krishnan Mahadevan

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



Roman Hiden

unread,
Sep 28, 2012, 5:42:34 PM9/28/12
to testng...@googlegroups.com
Thanks Krishnan for clarification you are correct. Moreover you can have different input sets. or you can specify the spread sheet name. or maybe filter that your data provider will apply

Sathishwaran Selvaraj

unread,
Oct 28, 2014, 11:42:35 AM10/28/14
to testng...@googlegroups.com, cbe...@gmail.com
Hi All,
Am facing the similar issue now,
Like i need to pass a parameter from Xml and DataProvider together how can i achieve this.
I want to combine these objects to an single data provider (The one which gets data from XML and the one which has excel imported values)
Please give me some ideas or example to do so.


Thanks,
Sathishwaran




On Thursday, November 24, 2011 4:13:24 PM UTC+5:30, Pavithra wrote:

jeevan

unread,
Oct 28, 2014, 2:53:30 PM10/28/14
to testng...@googlegroups.com, Cedric Beust
Why cant you do the same thing through executing TestNg programmatic-ally?
So in this way its easy to add the dynamic suite name etc..
Thanks,
Jeevan

--
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.
Reply all
Reply to author
Forward
0 new messages