1. 程式人生 > >APP接口自動化測試JAVA+TestNG(三)之HTTP接口測試實例

APP接口自動化測試JAVA+TestNG(三)之HTTP接口測試實例

ons ace src 沒有 app 9.png 轉載 image try

前言
前兩篇普及相關基礎知識後,本篇主要對舉例對國家氣象局接口自動化測試進行講解(Get請求及結果斷言),以達到自動化測試入門目的,除了前兩篇的一些了解外,需要有一定的JAVA知識(HTTP相關)。


目錄

3.1 HTTP接口(GET)測試實例
3.1.1 待測接口說明 3.1.2 新建JAVA工程
1.工程目錄說明 2.Common.java源碼 3.getCityWeathe.java源碼 4.URLConnection.java源碼
3.1.3 編寫測試用例
1.測試用例 2.簡化後的用例
3.1.4 執行測試用例

以下實例均為本次總結再次編寫,,如轉載還請保留出處與作者姓名Findyou,謝謝!


3.1.1 待測接口說明

1.國家氣象局天氣預報接口

例:北京市天氣

  1. 接口的址:http://www.weather.com.cn/data/cityinfo/101010100.html
  2. 請求方式:GET
  3. 請求結果:
技術分享 {
"weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp1": "15℃",
"temp2": "5℃",
"weather": "多雲",
"img1": "d1.gif",
"img2": "n1.gif",
"ptime": "08:00"
}
} 技術分享

2.測試目標

請求對應cityid代碼,返回的城市是否是預期城市。

3.1.2 新建JAVA工程

步驟同上一篇2.2.2.1新建JAVA工程,不再復述,如不懂請百度Eclipse新建工程

1.工程結構說明

技術分享

2.Common.java源碼

技術分享 package findyou.Interface;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class Common {
/**
* 解析Json內容
*
* @author Findyou
* @version 1.0 2015/3/23
* @return JsonValue 返回JsonString中JsonId對應的Value
**/
public static String getJsonValue(String JsonString, String JsonId) {
String JsonValue = "";
if (JsonString == null || JsonString.trim().length() < 1) {
return null;
}
try {
JSONObject obj1 = new JSONObject(JsonString);
JsonValue = (String) obj1.getString(JsonId);
} catch (JSONException e) {
e.printStackTrace();
}
return JsonValue;
}
}
技術分享


3.getCityWeathe.java源碼

技術分享 package findyou.Interface;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
public class getCityWeather {
private String url="";

public String geturl() {
return url;
}

public String getHttpRespone(String cityCode) throws IOException {
String line = "";
String httpResults = "";
url=("http://www.weather.com.cn/data/cityinfo/"
+ cityCode + ".html");
try {
HttpURLConnection connection = URLConnection
.getConnection(url);
DataOutputStream out = null;
// 建立實際的連接
connection.connect();
out = new DataOutputStream(connection.getOutputStream());
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
while ((line = reader.readLine()) != null) {
httpResults = httpResults + line.toString();
}
reader.close();
// 斷開連接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return httpResults;
}
}
技術分享

4.URLConnection.java源碼

技術分享 package findyou.Interface;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLConnection {
public static HttpURLConnection getConnection(String url){
HttpURLConnection connection = null;
try {
// 打開和URL之間的連接
URL postUrl = new URL(url);
connection = (HttpURLConnection) postUrl.openConnection();
// 設置通用的請求屬性
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Charset", "utf-8");
connection.setRequestProperty("Accept-Charset", "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
技術分享

3.1.3 編寫測試用例

1.測試用例(常見"二"一般的寫法)

技術分享 package findyou.testcase;
import java.io.IOException;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import findyou.Interface.Common;
import findyou.Interface.getCityWeather;
public class test {
public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
public static String cityCode="";
public static getCityWeather weather=new getCityWeather();

@Test(groups = { "BaseCase"})
public void getShenZhen_Succ() throws IOException{
exp_city="深圳";
cityCode="101280601";
Reporter.log("【正常用例】:獲取"+exp_city+"天氣成功!");
httpResult=weather.getHttpRespone(cityCode);
Reporter.log("請求地址: "+weather.geturl());
Reporter.log("返回結果: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
city=Common.getJsonValue(weatherinfo, "city");
Reporter.log("用例結果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
Assert.assertEquals(city,exp_city);
}

@Test(groups = { "BaseCase"})
public void getBeiJing_Succ() throws IOException{
exp_city="北京";
cityCode="101010100";
Reporter.log("【正常用例】:獲取"+exp_city+"天氣成功!");
httpResult=weather.getHttpRespone(cityCode);
Reporter.log("請求地址: "+weather.geturl());
Reporter.log("返回結果: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
city=Common.getJsonValue(weatherinfo, "city");
Reporter.log("用例結果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
Assert.assertEquals(city,exp_city);
}

@Test(groups = { "BaseCase"})
public void getShangHai_Succ() throws IOException{
exp_city="上海";
cityCode="101020100";
Reporter.log("【正常用例】:獲取"+exp_city+"天氣成功!");
httpResult=weather.getHttpRespone(cityCode);
Reporter.log("請求地址: "+weather.geturl());
Reporter.log("返回結果: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
city=Common.getJsonValue(weatherinfo, "city");
Reporter.log("用例結果: resultCode=>expected: " + exp_city + " ,actual: "+ city);
Assert.assertEquals(city,exp_city);
}
}
技術分享

2.簡化後的用例

如何返回值格式與請求格式固定,用例優化如下

技術分享 package findyou.testcase;
import java.io.IOException;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import findyou.Interface.Common;
import findyou.Interface.getCityWeather;
public class test {
public String httpResult= null, weatherinfo= null, city=null,exp_city = null;
public static String cityCode="";
getCityWeather weather=new getCityWeather();

@Test(groups = { "BaseCase"})
public void getShenZhen_Succ() throws IOException{
exp_city="深圳";
cityCode="101280601";
resultCheck(cityCode, exp_city);
}

@Test(groups = { "BaseCase"})
public void getBeiJing_Succ() throws IOException{
exp_city="北京";
cityCode="101010100";
resultCheck(cityCode, exp_city);
}

@Test(groups = { "BaseCase"})
public void getShangHai_Succ() throws IOException{
exp_city="上海";
cityCode="101020100";
resultCheck(cityCode, exp_city);
}

public void resultCheck(String cityCode_str, String exp_city_str) throws IOException{
Reporter.log("【正常用例】:獲取"+exp_city_str+"天氣成功!");
httpResult=weather.getHttpRespone(cityCode_str);
Reporter.log("請求地址: "+weather.geturl());
Reporter.log("返回結果: "+httpResult);
weatherinfo=Common.getJsonValue(httpResult, "weatherinfo");
city=Common.getJsonValue(weatherinfo, "city");
Reporter.log("用例結果: resultCode=>expected: " + exp_city_str + " ,actual: "+ city);
Assert.assertEquals(city,exp_city_str);
}
}
技術分享

工程下載地址:http://pan.baidu.com/s/1mhyTNny 密碼:11ft

3.1.4 執行測試用例

技術分享

技術分享


TestNG自動化測試系列實例,基本已完畢,Post方法由於篇幅問題,則再不貼出來了,了解了以上實例,Post方法沒有太大問題。後續如有時間看心情再上持續集成、自動化部署、自動化用例執行與測試報告輸出博文~~~

APP接口自動化測試JAVA+TestNG(三)之HTTP接口測試實例