java 与testng利用XML做数据源的数据驱动示例详解
程序员文章站
2024-03-09 08:07:17
java 与testng利用xml做数据源的数据驱动示例详解
testng的功能很强大,利用@dataprovider可以做数据驱动,数据源文件可以是excel,xml,...
java 与testng利用xml做数据源的数据驱动示例详解
testng的功能很强大,利用@dataprovider可以做数据驱动,数据源文件可以是excel,xml,yaml,甚至可以是txt文本。在这以xml为例:
备注:@dataprovider的返回值类型只能是object[][]与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> <testmethod4> <input>3</input> <button>4</button> </testmethod4> </data>
处用dom4j解析xml,parserxml.java文件:
package com.test; import java.io.file; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import org.dom4j.document; import org.dom4j.documentexception; import org.dom4j.element; import org.dom4j.io.saxreader; public class parserxml { public list parser3xml(string filename) { file inputxml = new file(filename); list list=new arraylist(); int count = 1; saxreader saxreader = new saxreader(); 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; } }
然后把解析出来的list转换成object[][]类型的数据,且结合在@dataprovider中。
testdata.java文件:
package com.test; import java.io.file; import java.lang.reflect.method; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import org.testng.annotations.dataprovider; public class testdata { private list l; public testdata() { this.getxmldata(); } public void getxmldata(){ parserxml p = new parserxml(); l = p.parser3xml(new file("src/com/test/testdata.xml").getabsolutepath()); } @dataprovider public object[][] providermethod(method method){ list<map<string, string>> result = new arraylist<map<string, string>>(); for (int i = 0; i < l.size(); i++) { map m = (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 = new object[result.size()][]; for(int i=0; i<result.size(); i++){ files[i] = new object[]{result.get(i)}; } return files; } }
再通过测试文件来测试一下:
testdataprovider.java文件:
package com.test; import java.util.map; import org.testng.annotations.*; public class testdataprovider extends testdata { @test(dataprovider="providermethod") public void 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"); } }
我们再回过头来分析一下xml文件,有两个testmethod1结点,testmethod2,testmethod3,testmethod4结点各一个,在testdataprovider.java文件中,定义了testmethod1,testmethod2,testmethod3,testmethod4四个测试函数,且testmethod4没有用到dataprovider,所以运行结果最后应该是testmethod1运行两遍,testmethod2,testmethod3,testmethod4各运行一遍,结果如下:
method1 received:1 method1 received:3 method2 received:3 method3 received:3 method4 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}) passed: testmethod4 =============================================== default test tests run: 5, failures: 0, skips: 0 ===============================================
也就是说通过这种方式,只需要把测试函数先写好,然后在xml文件中定义好数据就行了,就能控制函数是否运行,运行次数且运行的数据。
ok,let's try.....
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: asp.net 过滤图片标签的正则