优化testng
增加根据Excel接口测试案例读取内容进行发送http请求并将服务器返回值与Excel预期值进行比对
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import 包名.ExcelUtil;
import 包名.HttpRequestUtil;
import 包名.PatternUtil;
public class TestRun {
private String filePath = null;
@Parameters({ "filePathParam" })
@BeforeTest
public void beforeTest(String filePath) {
this.filePath = filePath;
}
@DataProvider(name = "testcasedata")
public Object[][] dp() throws Exception {
ExcelUtil excelUtil = new ExcelUtil(this.filePath);
return excelUtil.getArrayCellValue(0);
}
@Test(dataProvider = "testcasedata")
public void httpReq(String id,String isExec,String testCase,String reqType,String reqHost,
String reqInterface,String reqData,String expResult,String isDep,String depKey) throws Exception{
String reqUrl = reqHost + reqInterface;
String actResult = null;
Reporter.log("用例id" + id);
Reporter.log("用例描述" + testCase);
Reporter.log("请求方法" + reqType);
Reporter.log("请求接口" + reqUrl);
if("YES".equals(isExec)) {
if("POST".equals(reqType)) {
actResult = HttpRequestUtil.sendPost(reqUrl, reqData);
}else {
actResult = HttpRequestUtil.sendGet(reqUrl, reqData);
}
}else {
Reporter.log("不执行测试用例,因为Excel中Test_is_exec为NO");
}
PatternUtil.compareResult(expResult, actResult);
}
}
|