讲完json对象的操作,今天开始正式进入正题——接口测试。这里的接口指的是HTTP接口测试,主要的请求方法是​​GET​​和​​POST​​,下面开始讲​​GET​​请求的测试实践。

内容概述

以腾讯天气中一个获取某地天气信息详情的接口为案例,演示如何构造HTTP请求对象,如何把参数组装到请求对象中,如何解析响应结果,获取信息,进行校验等等。由于第三方接口,这里没有问题,代码比较简单,相当于文档了。

中间本来想讲讲断言的,发现很久不写,有点翻车,索性放弃了,各位见谅。

GET请求测试实践



gitee地址:https://gitee.com/fanapi/tester

测试代码

package com.fun;

import com.alibaba.fastjson.JSONObject;
import com.fun.frame.httpclient.FanLibrary;
import org.apache.http.client.methods.HttpGet;

publicclass WeacherTest extends FanLibrary {

public static void main(String[] args) {
String url = "https://wis.qq.com/weather/common";
JSONObject params = new JSONObject();
params.put("source", "pc");
params.put("province", "北京市");
params.put("city", "北京市");
params.put("county", "西城区");
params.put("weather_type", "observe|forecast_1h|forecast_24h|index|alarm|limit|tips|rise");
HttpGet httpGet = getHttpGet(url, params);
JSONObject response = getHttpResponse(httpGet);
output(response);
output("响应状态码:" + response.getInteger("status"));
// output("响应信息:" + response.getString("message"));
// JSONObject info = response.getJSONObject("data").getJSONObject("forecast_24h");
// Set<String> keySet = info.keySet();
// for (String key : keySet) {
// int max_degree = info.getJSONObject(key).getIntValue("max_degree");
// String time = info.getJSONObject(key).getString("time");
// output(time + "最高气温:" + max_degree + "摄氏度");
// }


testOver();
}


}

  • 郑重声明:文章首发于公众号“FunTester”,禁止第三方(腾讯云除外)转载、发表。

技术类文章精选

无代码文章精选

GET请求实践--测试框架视频讲解_性能测试