利用testng的注解来实现。
package com.lemon.apiAuto.cases;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.lemon.apiAuto.Util.AssertUtil;
import com.lemon.apiAuto.Util.ExcelUtil;
import com.lemon.apiAuto.Util.HttpUtil;
import com.lemon.apiAuto.pojo.Api;
import com.lemon.apiAuto.pojo.Case;
import com.lemon.apiAuto.pojo.WriteBackData;
/**
* 登陆
* @author 秀秀
*
*/
public class Login {
/**
* 登陆
* @param api
* @param case1
*/
@Test(dataProvider="login")
public void loginTest(Api api,Case case1) {
String uri=api.getUrl();
String type=api.getType();
String params=case1.getParams();
String contentType=api.getContentType();
//登陆接口,默认不需要鉴权
String response=HttpUtil.call(uri, type, params, contentType);
//response数据回写,回写到哪里去(excelPath,excelSheet,rowNum),可以把这些数据封装成对象。等用例执行完了,再一次性做write的操作
//caseid==回写的行号
int rowNum=Integer.parseInt(case1.getCaseId());
WriteBackData wbd=new WriteBackData(rowNum, response);
ExcelUtil.wbdList.add(wbd);
//判断预期结果是否与实际结果相符,封装成一个方法,放在assertUil类里面
String excpectedResult=case1.getExpectedResponseData();
AssertUtil.verification(excpectedResult,response);
}
/**
* class结束后,批量回写数据
*/
@AfterSuite
public void writeBackData() {
//回写的excel路径
// String excelPath=PropertiesUtil.getPath("excelPath");
//回写的sheet名称
String sheetName="用例";
//调用excelUtil类进行回写
ExcelUtil.writeBackData(sheetName, 5);
}
@DataProvider
public Object[][] login() {
Object [][] obj=ExcelUtil.getApiAndCaseByApiId("2");
return obj;
}
}