本文主要对举例对国家气象局接口自动化测试进行讲解(Get请求及结果断言),以达到自动化测试入门目的,除了前两篇的一些了解外,需要有一定的JAVA知识(HTTP相关)。
例:北京市天气
http://www.weather.com.cn/data/cityinfo/101010100.html
GET
{
"weatherinfo": {
"city": "北京",
"cityid": "101010100",
"temp1": "15℃",
"temp2": "5℃",
"weather": "多云",
"img1": "d1.gif",
"img2": "n1.gif",
"ptime": "08:00"
}
}
请求对应cityid代码,返回的城市是否是预期城市。
package com.test.interfaces.demo;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
public class Common {
/**
* 解析Json内容
*
* @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;
}
}
package com.test.interfaces.demo;
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);
// 建立实际的连接
connection.connect();
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;
}
}
package com.test.interfaces.demo;
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;
}
}
如何返回值格式与请求格式固定,用例优化如下:
package com.test.interfaces.demo;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Test;
import java.io.IOException;
public class TestWeataher {
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);
}
}
本文完~~~