Java combined with testng, data-driven examples of data sources using XML

Source: Internet
Author: User
Tags testng

TestNG is very powerful, @dataprovider can be used to do data-driven, data source files can be excel,xml,yaml, even can be txt text. In this case, take XML as an example:

Note: The return value type of the @DataProvider can only be object[][] and iterator<object>[]

Testdata.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Data><TestMethod1><Input>1</Input><button>2</button></TestMethod1><TestMethod1><Input>3</Input><button>4</button></TestMethod1><Testmethod2><Input>3</Input><button>4</button></Testmethod2><Testmethod3><Input>3</Input><button>4</button></testmethod3> " Span style= "COLOR: #0000ff" ><testmethod4> <input>3</input> <button>4</button> </testmethod4></data>      

Parse the Xml,parserxml.java file with dom4j:

PackageCom.test;ImportJava.io.File;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.List;ImportJava.util.Map;ImportOrg.dom4j.Document;ImportOrg.dom4j.DocumentException;ImportOrg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;PublicClassParserxml {PublicList parser3xml (String fileName) {File Inputxml =NewFile (FileName); List list=NewArrayList ();int count = 1; Saxreader Saxreader =NewSaxreader ();Try{Document document =Saxreader.read (Inputxml); Element employees =Document.getrootelement ();For (Iterator i = employees.elementiterator (); I.hasnext ();) {Element employee = (Element) I.next (); Map map = new HashMap (); Map Tempmap = new HashMap (); For (Iterator j = employee.elementiterator (); J.hasnext ();) {element node = (Element) J.next (); Tempmap.put (Node.getname (), Node.gettext ());} map.put (Employee.getname (), TEMPMAP); List.add (map); }} catch (Documentexception e) {System.out.println (E.getmessage ());} return list;}}             

The parsed list is then converted to object[][] type data, and is combined in @dataprovider.

Testdata.java file:

PackageCom.test;ImportJava.io.File;ImportJava.lang.reflect.Method;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.List;ImportJava.util.Map;ImportOrg.testng.annotations.DataProvider;PublicClassTestData {PrivateList l;PublicTestData () {This. Getxmldata (); }PublicvoidGetxmldata () {Parserxml p =NewParserxml (); L = P.parser3xml (New File ("Src/com/test/testdata.xml"). GetAbsolutePath ()); } @DataProviderPublicObject[][] Providermethod (method) {list<map<string, string>> result =New Arraylist<map<string, string>> (); for (int i = 0; i < l.size (); I++ (MAP) l.get (i); if (M.containskey (Method.getname ())) {map<string, string> DM = (map<string, String>new Object[result.size () []; for (int i=0; i<result.size (); i++ ) {Files[i] = new object[]{ Result.get (i)}; } return files;}}       

Test the test file again:

Testdataprovider.java file:

PackageCom.test;ImportJava.util.Map;Import org.testng.annotations.*;PublicClass TestdataproviderExtendsTestData {@Test (dataprovider= "Providermethod")Publicvoid TestMethod1 (map<?,? >param) {System.out.println ("Method1 Received:" +param.get ("input")); } @Test (dataprovider= "Providermethod") public void Testmethod2 (map<?,? > param) { System.out.println ("Method2 Received:" +param.get ("input"));} @Test (dataprovider= "Providermethod") public void testmethod3 (map<?,? > param) { System.out.println ("Method3 Received:" +param.get ("input"));} @Test public void testmethod4 () {System.out.println ("method4 received:4");}}    

Let's go back and analyze the XML file, there are two TESTMETHOD1 nodes, one for each of the TESTMETHOD2,TESTMETHOD3,TESTMETHOD4 nodes, and in the Testdataprovider.java file, define the TESTMETHOD1,TESTMETHOD2,TESTMETHOD3,TESTMETHOD4 four test functions, and TESTMETHOD4 does not use Dataprovider, So the running result should be testmethod1 run two times, testmethod2,testmethod3,testmethod4 each run again, the results are as follows:

Method1 received:1method1 received:3method2 received:3method3 received:3method4 received:4 Passed:testmethod1 ({input=1, button=2}) passed:testmethod1 ({input=3, button=4}) passed:testmethod2 ({input= 3, Button=4}) passed:testmethod3 ({input=3, button=4         

Translated from: http://www.cnblogs.com/zhangfei/archive/2012/10/10/2718989.html

Java combined with testng, data-driven examples of data sources using XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.