接口测试 简化接口测试的一些工具

coolfish · 2017年01月16日 · 2068 次阅读

一、简介

在接口自动化实践过程中,已有很多工具,此处不再赘述。但为了灵活性,大家往往会选择自己写脚本,在自己实现的过程中,会有很多重复的代码,我把一些常用功能做了一些简单封装,话不多说,直接上 demo(我使用的语言为 python,目前只支持 python 2.7)

二、定义 API 请求

使用说明

from pithy import request, make_session

# 定义,method默认为get, data_type默认为json,使用时可按需传入
@request(url='请求的API的host + path', method='http method', data_type='json or form')
def sample(var1, var2):
    """
    header 为请求的HEADERS字典
    params 为请求url上的参数
    body 为body参数
    """
    headers = {'xx': xx}
    params = {'xx': var1}
    body = {'xx': var2}

# 使用
result = sample(var1, var2).to_json()
result = sample(var1, var2).to_content()
result = sample(var1, var2).get_cookie()


# 多个请求使用同一个session
session = make_session()
sample(var1, var2, session=session).to_json()

HTTP REST GET 请求

from pithy import request

# 定义
@request(url='http://api.k780.com:88')
def get_weather_by_coordinates(weaid):
    params = {'app': 'weather.future',
              'weaid': weaid,
              'appkey': '22777',
              'sign': '6d3b280c53912154b0d3cbdcc86c6f2a',
              'format': 'json'}


# 获取json响应
json_result = get_weather_by_coordinates('beijing').to_json()

# 通过点号取字典值
result = json_result.result

# 通过object path(objectpath文档地址:http://objectpath.org/reference.html#CompleteSyntax)
# 取高温大于1, 低温大于-5的天
which_days = json_result('$.result[int(@.temp_high)>1 and int(@.temp_low>-5)].week')
for i in which_days:
    print i

日志输出

******************************************************

1 接口描述:
get_weather_by_coordinates

2 请求url
url: http://api.k780.com:88

3 请求方法
method: get

4 请求headers:
{
    "Accept": "application/json, text/plain, */*",
    "Content-Type": "application/json"
}

5 请求url参数:
{
    "app": "weather.future",
    "appkey": "22777",
    "format": "json",
    "sign": "6d3b280c53912154b0d3cbdcc86c6f2a",
    "weaid": "beijing"
}

6 响应结果:
{
    "result": [
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-16",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "1",
            "temp_low": "-5",
            "temperature": "1℃/-5℃",
            "weaid": "1",
            "weather": "霾",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/53.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/53.gif",
            "weatid": "33",
            "weatid1": "33",
            "week": "星期一",
            "wind": "南风转北风",
            "windid": "29",
            "winp": "微风",
            "winpid": "125"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-17",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "5",
            "temp_low": "-6",
            "temperature": "5℃/-6℃",
            "weaid": "1",
            "weather": "霾转晴",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/53.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/0.gif",
            "weatid": "33",
            "weatid1": "1",
            "week": "星期二",
            "wind": "北风",
            "windid": "20",
            "winp": "微风",
            "winpid": "125"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-18",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "2",
            "temp_low": "-5",
            "temperature": "2℃/-5℃",
            "weaid": "1",
            "weather": "晴转阴",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/0.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/2.gif",
            "weatid": "1",
            "weatid1": "3",
            "week": "星期三",
            "wind": "南风转北风",
            "windid": "29",
            "winp": "微风",
            "winpid": "125"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-19",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "0",
            "temp_low": "-9",
            "temperature": "0℃/-9℃",
            "weaid": "1",
            "weather": "阴转晴",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/2.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/0.gif",
            "weatid": "3",
            "weatid1": "1",
            "week": "星期四",
            "wind": "西北风转北风",
            "windid": "61",
            "winp": "4-5级转3-4级",
            "winpid": "54"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-20",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "0",
            "temp_low": "-8",
            "temperature": "0℃/-8℃",
            "weaid": "1",
            "weather": "晴转多云",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/0.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/1.gif",
            "weatid": "1",
            "weatid1": "2",
            "week": "星期五",
            "wind": "西南风转北风",
            "windid": "92",
            "winp": "微风",
            "winpid": "125"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-21",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "3",
            "temp_low": "-7",
            "temperature": "3℃/-7℃",
            "weaid": "1",
            "weather": "多云转晴",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/1.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/0.gif",
            "weatid": "2",
            "weatid1": "1",
            "week": "星期六",
            "wind": "西北风",
            "windid": "15",
            "winp": "微风",
            "winpid": "125"
        },
        {
            "cityid": "101010100",
            "citynm": "北京",
            "cityno": "beijing",
            "days": "2017-01-22",
            "humi_high": "0",
            "humi_low": "0",
            "humidity": "0℉/0℉",
            "temp_high": "3",
            "temp_low": "-7",
            "temperature": "3℃/-7℃",
            "weaid": "1",
            "weather": "晴",
            "weather_icon": "http://api.k780.com:88/upload/weather/d/0.gif",
            "weather_icon1": "http://api.k780.com:88/upload/weather/n/0.gif",
            "weatid": "1",
            "weatid1": "1",
            "week": "星期日",
            "wind": "西北风",
            "windid": "15",
            "winp": "微风",
            "winpid": "125"
        }
    ],
    "success": "1"
}
星期二
星期三
星期六
星期日

三 定义 DB

这里使用 sqlalchemy orm,使用方法如下:

# 注意,不支持Sharding数据库
from pithy import db
sample_db= db("mysql://user:password@host:port/database?charset=utf8", 'sample_db')

# 以下为测试代码
from sample_db import table1, table2
query_result = session.query(table1).all()

# 如不使用导入功能,可使用下面的方法
table1 = sample_db.table1

四 代码地址

https://github.com/yuyu1987/pithy

下午有点忙,稍后再来完善

共收到 4 条回复 时间 点赞

断言跟测试数据是怎么设计的?楼主

#1 楼 @yiwang 断言推荐使用 python 单测框架自带的,比如 unittest、pytest、nose 等,测试数据可根据自己的需求,怎么方便怎么来

coolfish 关于接口测试框架 中提及了此贴 01月16日 14:21

学习了~

匿名 #1 · 2017年02月09日

想请教下,你在做接口测试返回结果的判断时,是肉眼看输出或者查数据库,还是代码判断返回值(如果是代码判断返回值,是所有字段都去校验吗还是校验部分)?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册