How to pass parameters to dataProvider from TestNG.xml

2,053 views
Skip to first unread message

Ben Mark

unread,
Jan 23, 2017, 9:02:53 AM1/23/17
to testng-users
 I have managed to only accomplish this with the following testng.xml sample:
The following testng.xml represents test_A running with 2 iterations with 1 parameter "data-id":

- 1st iteration value = 1
- 2nd iteration value = 2.


DataProvider:
    @DataProvider(name = "dp", parallel = true)
    public Object[][] createData(Method m, ITestContext ctx) throws Exception {
        for (XmlClass test : ctx.getCurrentXmlTest().getXmlClasses()) {
            for (XmlInclude method : test.getIncludedMethods()) {
                if (method.getName().equals(m.getName())) {
                    int temp = Integer.parseInt(method.getAllParameters().get("data-id"));
                    return new Object[][] { { temp } };
                }
            }
        }
        return null;
    }
TestNG.xml:
<suite name="Default Suite" parallel="methods">
    <test name="A">
        <classes>
            <class name="Design.BaseTest2.ChildTestClass">
                <methods>
                    <include name="test_A">
                        <parameter name="data-id" value="1" />
                    </include>
                </methods>
            </class>
        </classes>
    </test>
    <test name="B">
        <classes>
            <class name="Design.BaseTest2.ChildTestClass">
                <methods>
                    <include name="test_A">
                        <parameter name="data-id" value="2" />
                    </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>




Is there a better way of achieving such functionality from testng.xml?
The issue here is *usability*.
It is unthinkable to create such a design where for every testcase iteration I'll need to create 6 xml nodes just to gain another testcase iteration:
1) <test>
2) <classes>
3) <class>
4) <methods>
5) <include>
6) <parameter>


thanks in advance,
please advise,

-Ben

Jon Nelson

unread,
Jan 23, 2017, 10:29:25 AM1/23/17
to testng-users
The 'better way' would be to NOT pass this data from testng.xml, but rather specify data inline (in the data provider) or read it from data files... why do you need it in testng.xml?

Ben Mark

unread,
Jan 23, 2017, 11:08:29 AM1/23/17
to testng-users


On Monday, 23 January 2017 17:29:25 UTC+2, Jon Nelson wrote:
The 'better way' would be to NOT pass this data from testng.xml, but rather specify data inline (in the data provider) or read it from data files... why do you need it in testng.xml?

 
The TestNG.xml gives me the ability to declare a test playlist*, what tests are going to be executed and with the example above also state how many iterations and which parameters
It looks to me like a very straight forward usable execution way.
What you're offering to me is to have 2 files that must be changed whenever I need to change:
 - which tests will run
 - how many times (how many test cases injected into the DataProvider)
 - parameters

I'd like to have these configurations in 1 xml file, call it however you like, as far as Ive read, it all belongs to testng.xml

-Ben

Ben Mark

unread,
Jan 23, 2017, 11:18:07 AM1/23/17
to testng-users


On Monday, 23 January 2017 17:29:25 UTC+2, Jon Nelson wrote:
The 'better way' would be to NOT pass this data from testng.xml, but rather specify data inline (in the data provider) or read it from data files... why do you need it in testng.xml?


Note: I'm running these tests on a remote server which I feed it via source control.
All I want is to commit 1 file and 1 file only

-Ben

Jessie

unread,
Jan 23, 2017, 5:01:17 PM1/23/17
to testng...@googlegroups.com
Hi Jon,

If you use dataprovider, how do you handle the test case name? 
I'm trying this one: https://groups.google.com/forum/#!topic/testng-users/8ncZt1ga7Bs. But it didn't work.





On Mon, Jan 23, 2017 at 10:29 AM, Jon Nelson <jondavi...@comcast.net> wrote:
The 'better way' would be to NOT pass this data from testng.xml, but rather specify data inline (in the data provider) or read it from data files... why do you need it in testng.xml?

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Jon Nelson

unread,
Jan 24, 2017, 10:56:40 AM1/24/17
to testng-users
Perhaps my needs are simpler than yours, but this technique (using testng.xml) seems like a strange & unusual way to specify test data.  Generally, the data provider (or data' file) specifies both the parameters and # of iterations (data objects), and the @DataProvider annotation identifies which tests use that data (which generally doesn't change) - so you should only need to commit 1 file to update your test data.  Parameters passed from testng.xml are generally 'metadata' (e.g. browser, platform) rather than actual test data.

@Yanan, I'm not sure what you mean by 'handle the test case name'; I generally use <test> to specify one or more test classes to execute/report together, not to specify individual testcases within a class.
Reply all
Reply to author
Forward
0 new messages