接口測試痛點(開發(fā)新東方四六級所遇到的)
- 初期開發(fā)階段后臺前端交流扯皮
-
只有接口文檔侯养,沒有測試數(shù)據(jù)或測試數(shù)據(jù)不全
受開發(fā)人員嚴謹與否等因素,文檔中字段與代碼中字段或者結構是有小區(qū)別的型檀,導致你封裝接口數(shù)據(jù)戰(zhàn)戰(zhàn)兢兢(許多時候為避免返工,直接進入開發(fā)停滯狀態(tài)或者只封裝部分重要接口) -
后臺接口頻繁掛掉
開發(fā)初期極其常見且頻繁露泊,讓你不停的聯(lián)系接口開發(fā)人員掌测、導致工作打斷(如果開發(fā)人員臨時不在或有事霎桅,開發(fā)陷入停滯) -
服務只在內網(wǎng) 沒有離線數(shù)據(jù)
因網(wǎng)絡原因導致前端開發(fā)工作停滯(或者另一種場景栖疑,產(chǎn)品經(jīng)理等其它相關項目人員想在家或者非上班狀態(tài)離線測試產(chǎn)品及跟蹤其進度) -
徹底的無底線的 淪為后臺人員,產(chǎn)品經(jīng)理滔驶,測試人員的小白鼠
現(xiàn)在服務是不是掛了遇革?接口通不通?你參數(shù)傳的什么揭糕?等會我打個斷點萝快,你調用一下?返回數(shù)據(jù)是什么著角?有沒有問題揪漩,導致你不停的點擊、頻繁的打包吏口、啟動app奄容,只為了回答他們一個簡單的問題,配合他們一個簡單的操作产徊。讓其他同事肆意的打斷你的工作
解決實踐
一昂勒、思路描述#####
- 接口未開發(fā)階段:采用rap,進行接口地址的配置舟铜,參數(shù)的封裝
- 接口部署階段:
- 離線數(shù)據(jù)解決:采用fiddler代理抓包戈盈,并導出數(shù)據(jù),配置到assets目錄谆刨,攔截http請求(如果是https,需要設置證書并導出到手機)塘娶,返回本地相應接口的離線數(shù)據(jù)
- 接口測試需求:對每個接口進行單元測試封裝,如果fiddler有記錄的話也可以直接點擊發(fā)送請求
二痊夭、實際操作#####
安裝fiddler 設置代理刁岸,為了避免無用數(shù)據(jù),設置filtters生兆,一般把服務器域名配置上就可以了
手機或者電腦上設置代理到 代理服務器
寫單元測試 封裝接口(此處忽略)
-
導出fiddler里面所有數(shù)據(jù)难捌,并放置到assets目錄,如下圖所示
Paste_Image.png 寫okhttp攔截器鸦难,讀取本地離線數(shù)據(jù):
public class LocalJsonInterceptor implements Interceptor{
public static final int SECOND = 1000;
/**
* 設置網(wǎng)絡延時模擬 默認2秒
*/
public static long mockNetDelay = 2;
@Override
public Response intercept(Chain chain) throws IOException {
String responseString = createResponseBody(chain);
Response response = new Response.Builder()
.code(200)
.message(responseString)
.request(chain.request())
.protocol(Protocol.HTTP_1_0)
.body(ResponseBody.create(MediaType.parse("application/json"), responseString.getBytes()))
.addHeader("content-type", "application/json")
.build();
return response;
}
/**
* 讀文件獲取json字符串根吁,生成ResponseBody
*
* @param chain
* @return
*/
private String createResponseBody(Chain chain) {
HttpUrl uri = chain.request().url();
String path = uri.url().getPath();
String host = uri.host();
try{
if(mockNetDelay>0){
Thread.sleep(mockNetDelay*SECOND);
}
return getresponseString(host,path);
}catch (Exception e){
ResponseBean bean = new ResponseBean();
bean.setCode(-123);
bean.setMessage("呵呵---->>讀取本地json數(shù)據(jù)發(fā)生線程異常");
Gson gson = new Gson();
return gson.toJson(bean);
}
}
public String getresponseString(String host,String path){
try {
String subPath = path.substring(0,path.lastIndexOf("/"));
String method = path.substring(path.lastIndexOf("/")+1,path.length());
String [] methodJson = App.getInstance().getAssets().list(host+subPath);
Random random=new Random();
int index = 0;
// 隨機50次 能不能找到 看命 此處可以隨機同一接口不同場景下的返回數(shù)據(jù)
int i;
for(i= 0 ;i<50;i++){
index=random.nextInt(methodJson.length);
int findIndex = methodJson[index].indexOf("[");
if(findIndex>=0){
if(method.equals(methodJson[index].substring(0,findIndex))){
break;
}
}else if(method.equals(methodJson[index])){
break;
}
}
if(i == 50){
ResponseBean bean = new ResponseBean();
bean.setCode(-122);
bean.setMessage("呵呵---->>本地json數(shù)據(jù)未找到");
Gson gson = new Gson();
return gson.toJson(bean);
}
InputStream is = App.getInstance().getAssets().open(host+subPath+"/"+methodJson[index]);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer, "GB2312");
return text;
} catch (Exception e) {
ResponseBean bean = new ResponseBean();
bean.setCode(-121);
bean.setMessage("local---->>"+e.toString());
Gson gson = new Gson();
return gson.toJson(bean);
}
}
}