java結合testng,利用XML做資料來源的資料驅動樣本

來源:互聯網
上載者:User

標籤:too   system   int   解析xml   odi   blog   cat   copy   testng   

 

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:1method1 received:3method2 received:3method3 received:3method4 received:4PASSED: 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..... 

原文轉自:http://www.cnblogs.com/zhangfei/archive/2012/10/10/2718989.html

java結合testng,利用XML做資料來源的資料驅動樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.