Web接口自动化测试

Web接口自动化测试

接口的理解

接口就是API,可以理解为用来供外部调用(访问)的东东(模块)。

比如:

手机上可以查看实时天气(大概原理就是app应用调用了气象平台提供天气查询接口);12306网站的实名制认证(需要调用政府公安局相关信息系统提供的身份认证接口);

微信上绑定银行卡(需要调用银行系统提供的相关业务接口)。

 

JSON介绍

JSON是一种数据格式,任何对象都可以使用JSON数据来表示。

 可以表示某人,比如:

{
"name":"BillGates",
"street":"FifthAvenue New York 666",
"age":56,
"phone":"5551234567"
}

 可以表示网站,比如:

{"sites":[{"name":"菜鸟教程" , "url":"www.runoob.com"},{"name":"google" , "url":"www.google.com"},{"name":"微博" , "url":"www.weibo.com"}]}

等等。

 

注意:还有另外一种数据格式叫XML。

Web接口的访问

比如:

天气查看接口的地址为:http://weather.51wnl.com/weatherinfo/GetMoreWeather

 

参数列表如下:

参数名                       备注

cityCode                     城市编码

weatherType             查看类型[0:查看一周内的天气情况 1:查看当天的天气情况]

 

打开浏览器,输入如下地址:

http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101020300&weatherType=0

返回的json数据如下:

 

如果要格式化显示json数据的话,可以在火狐上安装格式化json数据的插件


或者,通过在线网站对json数据格式化显示


Web接口自动化测试

检查接口返回的城市和温度是否正确。

代码如下:

package day01;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class WeatherTest {
    private CloseableHttpClient httpClient;
    private int id;//用例编号
    private String city;//城市编号
    private int type;//查看类型  0:查看本周内的天气    1:查看当天的天气
    private String expectedCity;//期望值
    private String expectedTemp;//期望温度

    public WeatherTest(int id, String city, int type, String expectedCity, StringexpectedTemp) {
        super();
        this.id = id;
        this.city = city;
        this.type = type;
        this.expectedCity = expectedCity;
        this.expectedTemp = expectedTemp;
    }

    @After
    public void end() throws Exception{
        httpClient.close();//关闭客户端
    }

    @Parameters
    public static List data(){
        Object[][] arr = {
                {1,"101010100",1,"北京","-1"},
                {2,"101020100",1,"上海","5"},
                {3,"101280101",1,"广州","8"}
        };
        return Arrays.asList(arr);
    }

    @Test
    public void test() throws Exception{
            //接口地址
             String url ="http://weather.51wnl.com/weatherinfo/GetMoreWeather";
             //创建一个客户端用来访问接口
             httpClient= HttpClients.createDefault();
             //HttpClient httpClient = new DefaultHttpClient();
             //创建一个请求
            HttpGet httpGet =new HttpGet(url+"?cityCode="+city+"&weatherType="+type);       
            //发送该请求
            HttpResponse result =httpClient.execute(httpGet);
            //获取接口返回的内容
            String resData= EntityUtils.toString(result.getEntity());
            //将接口返回的内容转成JSON实例
            JSONObject json =new JSONObject(resData);
            //从JSON实例里面获取天气实例
            JSONObject weatherInfo =json.getJSONObject("weatherinfo"); 
            //验证返回的城市是否和期望值相等
            assertEquals(expectedCity,weatherInfo.getString("city"));
            //验证返回的温度是否和期望值相等
            assertEquals(expectedTemp,weatherInfo.getString("temp"));
    }
}


附截图:


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值