本篇主要对举例对国家⽓象局接⼝⾃动化测试进⾏讲解(Get请求及结果断⾔),以达到⾃动化测试⼊门⽬的,除了前两篇的⼀些了解外,需要有⼀定的JAVA知识(HTTP相关)。 1 待测接⼝说明
1.国家⽓象局天⽓预报接⼝
例:北京市天⽓
请求⽅式:GET
请求结果:
生态学杂志{
"weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp1": "15℃",
"temp2": "5℃",
"weather": "多云",
"img1": "d1.gif",
"img2": "n1.gif",
"ptime": "08:00"
}
}
2.测试⽬标
请求对应cityid代码,返回的城市是否是预期城市。 2 新建JAVA⼯程
1.⼯程结构说明
2.Common.java源码
package findyou.Interface;
马艺文
dehaus.jettison.json.JSONException;
dehaus.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 || im().length()
return null;
}
try {
JSONObject obj1 = new JSONObject(JsonString);
JsonValue = (String) String(JsonId);
} catch (JSONException e) {
e.printStackTrace();
}
return JsonValue;
}
}
package findyou.Interface;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.HttpURLConnection;
public class getCityWeather {
private String url="";
public String geturl() {
return url;
}
public String getHttpRespone(String cityCode) throws IOException { String line = "";
韩金凤
String httpResults = "";
url=("www.weather/data/cityinfo/"
+ cityCode + ".html");
try {
HttpURLConnection connection = URLConnection
.getConnection(url);
DataOutputStream out = null;
// 建⽴实际的连接
out = new OutputStream());
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader( InputStream()));
while ((line = adLine()) != null) {
httpResults = httpResults + String();
}
reader.close();
// 断开连接
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return httpResults;
清教徒的假面}
}
4.URLConnection.java源码
package findyou.Interface;
import java.HttpURLConnection;
import java.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.测试⽤例(常见"⼆"⼀般的写法)
stcase;
import java.io.IOException;
stng.Assert;
stng.Reporter;
stng.annotations.Test;
import findyou.Interface.Common;
import CityWeather;
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+"天⽓成功!");
HttpRespone(cityCode);
Reporter.log("请求地址: "+url());
中医美容学
Reporter.log("返回结果: "+httpResult);
JsonValue(httpResult, "weatherinfo");
JsonValue(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+"天⽓成功!");
HttpRespone(cityCode);
Reporter.log("请求地址: "+url());
Reporter.log("返回结果: "+httpResult);
JsonValue(httpResult, "weatherinfo");
JsonValue(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+"天⽓成功!");
HttpRespone(cityCode);
Reporter.log("请求地址: "+url());
Reporter.log("返回结果: "+httpResult);
JsonValue(httpResult, "weatherinfo");
JsonValue(weatherinfo, "city");