testNg自动化,读取excel的数据

自己写了一个testng执行excel用例的小程序,主要是运行.xlsx的,需要支持xls可以自己扩展,分享一下。下载地址:http://yun.baidu.com/share/link?shareid=3811093173&uk=925574576&third=0

需要引用的jar包有(demo里面也有这些jar包):

1、读取excel

    excel的数据放入List<Map<String, String>>中。这里,不包括excel第一条数据,因为第一条数据要作为map的key值。

    excel格式:

package com.milan.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {
    
    public static List<Map<String, String>> readXlsx(String fileName) {

        XSSFWorkbook xssfWorkbook=null;
        try {
            xssfWorkbook = new XSSFWorkbook(fileName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 循环工作表Sheet
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            // 循环行Row
            XSSFRow rowTitleRow =xssfSheet.getRow(0);
            for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                
                if (xssfRow == null) {
                    continue;
                }
                Map<String, String> map = new HashMap<String, String>();
                // 循环列Cell
                for (int cellNum = 0; cellNum <rowTitleRow.getLastCellNum(); cellNum++) {
                    XSSFCell xssfCell = xssfRow.getCell(cellNum);
                    XSSFCell xssfCellTitleCell = rowTitleRow.getCell(cellNum);
                    map.put(getValue(xssfCellTitleCell), getValue(xssfCell));
                }
                list.add(map);

            }
            return list;
    }
    @SuppressWarnings("static-access")
    private static String getValue(XSSFCell xssfCell) {
        if (xssfCell ==null){return ""; }
        if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(xssfCell.getBooleanCellValue());
        } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
            return String.valueOf(xssfCell.getNumericCellValue());
        } else {
            return String.valueOf(xssfCell.getStringCellValue());
        }
    }
}

2、解析excel的数据

   excel中,这个字段的值为y表示需要执行测试用例,如果为其他的,则表示不执行。

  字段中{$d}开头的表示用例说明。{$p}开头的,表示用例需要的预置参数。比如QQ好友发送消息,但是发送消息需要先登录,所以这里可以放登录的用户名和密码。

 

package com.milan.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CaseHelper {
    //根据excel的map 转换为数组 第一个为 入参 map 第二个为用例说明,第三个参数为执行用例的预置条件
    public static Object[] getObjArrByMap(Map<String,String> caseExcelMap){
        Map<String,String> caseParam = new HashMap<String,String>();
        Map<String,String> caseDesc = new HashMap<String,String>();
        Map<String,String> casePreset =new HashMap<String,String>();
        CaseInfo ci = new CaseInfo();
         for (String key : caseExcelMap.keySet()) {
             if (key.indexOf("{$d}")== 0){
                 caseDesc.put(key.replace("{$d}", ""), caseExcelMap.get(key));
             }
             else if(key.indexOf("{$p}") == 0){
                 casePreset.put(key.replace("{$p}", ""), caseExcelMap.get(key));
             }
             else {
                 String strValue = caseExcelMap.get(key);
                 if (!strValue.equals("")){
                     caseParam.put(key, strValue);
                 }
             }  
         }
         ci.setCaseDesc(caseDesc);
         ci.setCaseParam(caseParam);
         ci.setCasePreset(casePreset);
        

         return new Object[]{ci};
    }
    ///根据excel获取的list转换为  Object[][]
    public static Object[][] getObjArrByList(List<Map<String,String>> caseExcelList){
        List<Map<String,String>> caseExcuteList = getExcuteList(caseExcelList);
        Object[][] objArray = new Object[caseExcuteList.size()][];
        for(int i = 0;i<caseExcuteList.size();i++){
            objArray[i]=getObjArrByMap(caseExcuteList.get(i));
        }
        return objArray;
        
    }
    ///赛选出需要执行的用例
    private static List<Map<String,String>> getExcuteList(List<Map<String,String>> caseExcelList){
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        for( Map<String,String> m : caseExcelList){
        String str = m.get("{$d}isexcute").trim().toLowerCase();
        if (str.equals("y")){ 
                list.add(m);
            }
        }
        return list;
    }

}

 

3、用例类

用例类有3个属性,分别是参数,用例说明,预置参数。

package com.milan.utils;

import java.util.Map;

public class CaseInfo {
    ///{$d}isexcute 为y的时候表示需要执行
    
    //用例参数 在excel中知己以字段名开头
    private Map<String,String> caseParam;
    //用例说明 在excel中以{$d}开头
    private Map<String,String> caseDesc;
    //用例预置条件 在excel中以{$p}开头
    private Map<String,String> casePreset;
    
    public Map<String, String> getCaseParam() {
        return caseParam;
    }
    public void setCaseParam(Map<String, String> caseParam) {
        this.caseParam = caseParam;
    }
    public Map<String, String> getCaseDesc() {
        return caseDesc;
    }
    public void setCaseDesc(Map<String, String> caseDesc) {
        this.caseDesc = caseDesc;
    }
    public Map<String, String> getCasePreset() {
        return casePreset;
    }
    public void setCasePreset(Map<String, String> casePreset) {
        this.casePreset = casePreset;
    }
    
}

4、运行

package com.milan.test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.milan.utils.CaseHelper;
import com.milan.utils.CaseInfo;
import com.milan.utils.ReadExcel;


public class MyTest {
    protected String caseExcelPath =System.getProperty("user.dir")+"\\excel\\temp.xlsx";

    @DataProvider(name = "dataInfo")
    protected Object[][] dataInfo1() throws IOException {

        Object[][] myObj = null;
        List<Map<String, String>> list = ReadExcel.readXlsx(caseExcelPath);
        myObj = CaseHelper.getObjArrByList(list);
        return myObj;
    }
    @Test(dataProvider="dataInfo")
    public void testByExcel_Body(CaseInfo c) throws IOException{
        ///获取用例说明
        System.out.println(c.getCaseDesc());
        ///获取用例需要的参数
        System.out.println(c.getCaseParam());
        //获取执行用例需要的前置条件
        System.out.println(c.getCasePreset());
    }

}


5、输出结果:

{caseExpect=1, isexcute=y, caseDesc=发送消息}
{sendname=发送者名称, send=发送消息}
{login=登录字符串}

读取到excel的值之后,就可以自己加断言,自己去请求数据调方法等等。

testng断言失败,继续执行 http://blog.csdn.net/m1011566442/article/details/52084896

 testng代码执行  https://www.cnblogs.com/digod/p/6035177.html

public class Test2 {
    public static void main(String[] args) {
        //DefaultTest defaultTest = new DefaultTest();
        TestNG testNG = new TestNG();
        testNG.setTestClasses(new Class[]{DefaultTest.class});
        testNG.run();
    }
}
View Code

 

posted @ 2015-08-10 11:21  米蓝  阅读(6167)  评论(2编辑  收藏  举报