How to pass parameters to dataProvider from TestNG.xml

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to pass parameters to dataProvider from TestNG.xml

Ben Mark
 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass parameters to dataProvider from TestNG.xml

Jon Nelson-16
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass parameters to dataProvider from TestNG.xml

Ben Mark


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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass parameters to dataProvider from TestNG.xml

Ben Mark
In reply to this post by Jon Nelson-16


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

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass parameters to dataProvider from TestNG.xml

Jessie
In reply to this post by Jon Nelson-16
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to pass parameters to dataProvider from TestNG.xml

Jon Nelson-16
In reply to this post by Ben Mark
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.