程序员人生 网站导航

java结合testng,利用XML做数据源的数据驱动示例

栏目:php教程时间:2017-04-08 13:56:40

学习Java的同学注意了!!! 
学习进程中遇到甚么问题或想获得学习资源的话,欢迎加入Java学习交换群,群号码:183993990  我们1起学Java!


testng的功能很强大,利用@DataProvider可以做数据驱动,数据源文件可以是EXCEL,XML,YAML,乃至可以是TXT文本。在这以XML为例:

备注:@DataProvider的返回值类型只能是Object[][]与Iterator<Object>[]

TestData.xml:

复制代码
<?xml version="1.0" encoding="UTF⑻"?>
<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;
    }
    

}
复制代码

再通过测试文件来测试1下:

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");
    }

}
复制代码

我们再回过头来分析1下XML文件,有两个testmethod1结点,testmethod2,testmethod3,testmethod4结点各1个,在TestDataProvider.java文件中,定义了testmethod1,testmethod2,testmethod3,testmethod44个测试函数,且testmethod4没有用到dataProvider,所以运行结果最后应当是testmethod1运行两遍,testmethod2,testmethod3,testmethod4各运行1遍,结果以下:

复制代码
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.....
复制代码

学习Java的同学注意了!!! 
学习进程中遇到甚么问题或想获得学习资源的话,欢迎加入Java学习交换群,群号码:183993990  我们1起学Java!

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐