java 与testng利用XML做数据源的数据驱动示例详解
java与testng利用XML做数据源的数据驱动示例详解
testng的功能很强大,利用@DataProvider可以做数据驱动,数据源文件可以是EXCEL,XML,YAML,甚至可以是TXT文本。在这以XML为例:
备注:@DataProvider的返回值类型只能是Object[][]与Iterator<Object>[]
TestData.xml:
<?xmlversion="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> <testmethod4> <input>3</input> <button>4</button> </testmethod4> </data>
处用DOM4J解析XML,ParserXml.java文件:
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{ publicListparser3Xml(StringfileName){ FileinputXml=newFile(fileName); Listlist=newArrayList(); intcount=1; SAXReadersaxReader=newSAXReader(); try{ Documentdocument=saxReader.read(inputXml); Elementemployees=document.getRootElement(); for(Iteratori=employees.elementIterator();i.hasNext();){ Elementemployee=(Element)i.next(); Mapmap=newHashMap(); MaptempMap=newHashMap(); for(Iteratorj=employee.elementIterator();j.hasNext();){ Elementnode=(Element)j.next(); tempMap.put(node.getName(),node.getText()); } map.put(employee.getName(),tempMap); list.add(map); } }catch(DocumentExceptione){ System.out.println(e.getMessage()); } returnlist; } }
然后把解析出来的list转换成Object[][]类型的数据,且结合在@DataProvider中。
TestData.java文件:
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{ privateListl; publicTestData(){ this.getXmlData(); } publicvoidgetXmlData(){ ParserXmlp=newParserXml(); l=p.parser3Xml(newFile("src/com/test/TestData.xml").getAbsolutePath()); } @DataProvider publicObject[][]providerMethod(Methodmethod){ List<Map<String,String>>result=newArrayList<Map<String,String>>(); for(inti=0;i<l.size();i++){ Mapm=(Map)l.get(i); if(m.containsKey(method.getName())){ Map<String,String>dm=(Map<String,String>)m.get(method.getName()); result.add(dm); } } Object[][]files=newObject[result.size()][]; for(inti=0;i<result.size();i++){ files[i]=newObject[]{result.get(i)}; } returnfiles; } }
再通过测试文件来测试一下:
TestDataProvider.java文件:
packagecom.test; importjava.util.Map; importorg.testng.annotations.*; publicclassTestDataProviderextendsTestData{ @Test(dataProvider="providerMethod") publicvoidtestmethod1(Map<?,?>param){ System.out.println("method1received:"+param.get("input")); } @Test(dataProvider="providerMethod") publicvoidtestmethod2(Map<?,?>param){ System.out.println("method2received:"+param.get("input")); } @Test(dataProvider="providerMethod") publicvoidtestmethod3(Map<?,?>param){ System.out.println("method3received:"+param.get("input")); } @Test publicvoidtestmethod4(){ System.out.println("method4received:4"); } }
我们再回过头来分析一下XML文件,有两个testmethod1结点,testmethod2,testmethod3,testmethod4结点各一个,在TestDataProvider.java文件中,定义了testmethod1,testmethod2,testmethod3,testmethod4四个测试函数,且testmethod4没有用到dataProvider,所以运行结果最后应该是testmethod1运行两遍,testmethod2,testmethod3,testmethod4各运行一遍,结果如下:
method1received:1 method1received:3 method2received:3 method3received:3 method4received: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}) PASSED:testmethod4 =============================================== Defaulttest Testsrun:5,Failures:0,Skips:0 ===============================================
也就是说通过这种方式,只需要把测试函数先写好,然后在XML文件中定义好数据就行了,就能控制函数是否运行,运行次数且运行的数据。
OK,Let'stry.....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!