接口自动化测试实战核心代码(数据驱动 poi+TestNG)

 数据驱动,用poi组件读取excel中的用例,放到一个二维数组,用于TestNG框架 dataProviderClass作为测试数据;

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

用例模板:

执行结果:

 

package com.test.dataprovider;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ExcelUtils {

	public static void main(String[] args) throws Exception {
		Object[][] objects = ExcelUtils.getData("E:\\yuntu-retail/fly.xlsx", "customermodify");

		System.out.println(Arrays.deepToString(objects));
		for (Object[] data : objects) {
			for (Object datas : data) {
				System.out.print(datas + "\t");

			}
			System.out.println();
		}


	}

	
	public static Object[][] getData(String excelfilepath,String sheetname) throws Exception {
		
		
		FileInputStream fileInputStream=new FileInputStream(new File(excelfilepath));
		
		Workbook workbook=null;
		
		
		String filextensionName=excelfilepath.substring(excelfilepath.indexOf("."));
		
		
		System.out.println("文件:"+filextensionName);
		if (filextensionName.endsWith(".xlsx")) {
			
			workbook=new XSSFWorkbook(fileInputStream);
			
		}else if (filextensionName.endsWith(".xls")) {
			
			workbook=new HSSFWorkbook(fileInputStream);
		}else {
			System.out.println("excel文件错误");
			
		}
		
		Sheet sheet=workbook.getSheet(sheetname);
		
		int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
		
		
		List<String[]> records=new ArrayList<>();
		
		
		for (int i = 1; i <=rowcount; i++) {
			
			Row row=sheet.getRow(i);
			
			String fields[]=new String[row.getLastCellNum()];
			
			String ifrun=row.getCell(row.getLastCellNum()-1).getStringCellValue();
			
			
			switch (ifrun) {
			case ("y"):
				
				System.out.println(
						"No:" + row.getRowNum() + "Row" + "\t" + "casenumber:" + "\t" + row.getCell(0) + "will be tested");

				for (int j = 0; j < row.getLastCellNum(); j++) {
					
					row.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK).setCellType(CellType.STRING);
					fields[j] = row.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK).getStringCellValue();
	

				}

				records.add(fields);
				
				break;

			case ("n"):
				System.out.println(
						"No:" + row.getRowNum() + "Row"+ "skip test" + "\t" + "casenumuber:" +"\t"+ row.getCell(0) + "Not Run");
				
				
				break;
				
				
				
			case ("pass"):
				System.out.println(
						"No:" + row.getRowNum() + "Row"+ "skip test" + "\t" + "casenumuber:" +"\t"+ row.getCell(0) + "Not Run");
				
				
				break;	
				
				
				
				
			case ("fail"):

				System.out.println("No:" + row.getRowNum() + "Row" + "\t" + "casenumuber:" + "\t"+ row.getCell(0)
						+ "Executed(failed), please reset");

				break;
			default:
				System.out.println("No:" + row.getRowNum() + "Row" + "\t" + "casenumuber:" + "\t"+ row.getCell(0)
						+ "Execution result design error, please check");
				break;
			}	
				
				
		}
		
		
		Object[][] results=new String[records.size()][];
		
		for (int i = 0; i < results.length; i++) {
			results[i]=records.get(i);
		}
		
		
		return results;
		
	}
	
	
	
	
	
}

 

package com.test.dataprovider;

import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;


public class DataProviderClass {


	public static  String excelpath;
	
	
	public String getExcelpath() {
		return excelpath;
	}


	public static  void setExcelpath(String excelpath) {
		DataProviderClass.excelpath = excelpath;
	}
	
	
	@DataProvider(name="CommonData")
	public Object[][] GetTestData(Method method) throws Exception {
		
		Object[][] result=ExcelUtils.getData(getExcelpath(), method.getName());

	
		return result;
		
	}
	
	
	
	
	
	
	
	

}
package com.test.dataprovider;

import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.testng.annotations.BeforeSuite;
public class NewTest1 <T extends Object>{
	static String excelpath="E:\\yuntu-retail/fly.xlsx";
	
	
	@BeforeSuite
	public  void BeforeSuite() {
		DataProviderClass.excelpath="E:\\yuntu-retail/fly.xlsx";
	}

  
  
  @Test(dataProvider = "CommonData", dataProviderClass=DataProviderClass.class)
  public void customermodify(Object ...params ) {
	 
		List<Object> list =new ArrayList<>();

		

			for (Object t : params) {

				list.add(t);
			}

			
		
		
		System.out.println(list);
		
		System.out.println("\n");
	  
  }
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值