数据驱动概念:
数据驱动测试用设计步骤:
代码示例:
testNG DataProvider接口测试实践:
测试数据结构:
整体测试用例Demo:
@Test(dataProvider= ExcelDataProvider.providerName,dataProviderClass= ExcelDataProvider.class,description="登陆测试集")
public void accountInfoTest(CaseInfo c){
//获取接口名称
url = ExcelDataProvider.getRequestUrl(c);
//获取用例需要的参数
requestparam = ExcelDataProvider.getRequestParam(c);
//发起请求请求
String res = APIMthod.sendPostByHttpCient(url, requestparam);
//获取预期结果
String expect = ExcelDataProvider.getExpect(c);
//断言
Assert.assertEquals(expect, actual);
}
数据参数化:
public class ExcelDataProvider extends CaseHelper{
public static String caseExcelPath = constant.caseExcelPath;
public static String sheetName;
public static String domain;
public static final String providerName = "excelDataProvider";
@SuppressWarnings("static-access")
public ExcelDataProvider(String dataPath, String sheetName, String domain) {
this.caseExcelPath = dataPath;
this.sheetName = sheetName;
this.domain = domain;
}
@DataProvider(name = providerName)
public static Object[][] dataInfo(Method m) throws IOException {
Object[][] object = null;
List
public class CaseHelper {
// 根据文件的map 转换为数组 第一个为 入参 map 第二个为用例说明,第三个参数为执行用例的预置条件
private static Object[] getObjArrByMap(Map<String, String> caseInfoMap) {
Map<String, String> caseNo = new HashMap<String, String>();
Map<String, String> caseName = new HashMap<String, String>();
Map<String, String> caseDesc = new HashMap<String, String>();
Map<String, String> caseUrl = new HashMap<String, String>();
Map<String, String> caseParam = new HashMap<String, String>();
Map<String, String> caseExpect = new HashMap<String, String>();
CaseInfo ci = new CaseInfo();
for (String key : caseInfoMap.keySet()) {
if (key.equals(constant.caseNo)) {
caseNo.put(key, caseInfoMap.get(key));
}
if (key.equals(constant.caseName)) {
caseName.put(key, caseInfoMap.get(key));
}
if (key.equals(constant.caseDesc)) {
caseDesc.put(key, caseInfoMap.get(key));
}
if (key.equals(constant.caseUrl)) {
caseUrl.put(key, caseInfoMap.get(key));
}
if (key.equals(constant.caseParam)) {
caseParam.put(key, caseInfoMap.get(key));
}
if (key.equals(constant.caseExpect)) {
caseExpect.put(key, caseInfoMap.get(key));
}
}
ci.setCaseNo(caseNo);
ci.setCaseName(caseName);
ci.setCaseDesc(caseDesc);
ci.setCaseUrl(caseUrl);
ci.setCaseParam(caseParam);
ci.setCaseExpect(caseExpect);
return new Object[] { ci };
}
// /获取的list转换为 Object[][]
public static Object[][] getObjArrByList(List
数据utils:
public class CaseInfo {
//测试用例序号
private Map<String,String> caseNo;
//测试用例名称(与case中方法名一致)
private Map<String,String> caseName;
//测试用例描述
private Map<String,String> caseDesc;
//测试用例url
private Map<String,String> caseUrl;
//测试用例参数
private Map<String,String> caseParam;
//预期结果(断言)
private Map<String,String> caseExpect;
//是否执行
private Map<String,String> caseIsExec;
public Map<String, String> getCaseNo() {
return caseNo;
}
public void setCaseNo(Map<String, String> caseNo) {
this.caseNo = caseNo;
}
public Map<String, String> getCaseName() {
return caseName;
}
public void setCaseName(Map<String, String> caseName) {
this.caseName = caseName;
}
public Map<String, String> getCaseDesc() {
return caseDesc;
}
public void setCaseDesc(Map<String, String> caseDesc) {
this.caseDesc = caseDesc;
}
public Map<String, String> getCaseUrl() {
return caseUrl;
}
public void setCaseUrl(Map<String, String> caseUrl) {
this.caseUrl = caseUrl;
}
public Map<String, String> getCaseParam() {
return caseParam;
}
public void setCaseParam(Map<String, String> caseParam) {
this.caseParam = caseParam;
}
public Map<String, String> getCaseExpect() {
return caseExpect;
}
public void setCaseExpect(Map<String, String> caseExpect) {
this.caseExpect = caseExpect;
}
public Map<String, String> getCaseIsExec() {
return caseIsExec;
}
public void setCaseIsExec(Map<String, String> caseIsExec) {
this.caseIsExec = caseIsExec;
}
public String toString(){
return getCaseDesc().get(constant.caseDesc) + "|" + getCaseUrl() + "," + getCaseParam() ;
}
}