使用python做接口自动化测试容易吗

如题所述

为什么要做接口自动化测试?
在当前互联网产品迭代频繁的背景下,回归测试的时间越来越少,很难在每个迭代都对所有功能做完整回归。但接口自动化测试因其实现简单、维护成本低,容易提高覆盖率等特点,越来越受重视。
为什么要自己写框架呢?
使用Postman调试通过过直接可以获取接口测试的基本代码,结合使用requets + unittest很容易实现接口自动化测试的封装,而且requests的api已经非常人性化,非常简单,但通过封装以后(特别是针对公司内特定接口),可以进一步提高脚本编写效率。
一个现有的简单接口例子
下面使用requests + unittest测试一个查询接口
接口信息如下
请求信息:
Method:POST
URL:api/match/image/getjson
Request:
{
"category": "image",
"offset": "0",
"limit": "30",
"sourceId": "0",
"metaTitle": "",
"metaId": "0",
"classify": "unclassify",
"startTime": "",
"endTime": "",
"createStart": "",
"createEnd": "",
"sourceType": "",
"isTracking": "true",
"metaGroup": "",
"companyId": "0",
"lastDays": "1",
"author": ""
}

Response示例:
{
"timestamp" : xxx,
"errorMsg" : "",
"data" : {
"config" : xxx
}

Postman测试方法见截图:

测试思路
1.获取Postman原始脚本
2.使用requests库模拟发送HTTP请求**
3.对原始脚本进行基础改造**
4.使用python标准库里unittest写测试case**
原始脚本实现
未优化
该代码只是简单的一次调用,而且返回的结果太多,很多返回信息暂时没用,示例代码如下
import requests

url = "http://cpright.xinhua-news.cn/api/match/image/getjson"

querystring = {"category":"image","offset":"0","limit":"30","sourceId":"0","metaTitle":"","metaId":"0","classify":"unclassify","startTime":"","endTime":"","createStart":"","createEnd":"","sourceType":"","isTracking":"true","metaGroup":"","companyId":"0","lastDays":"1","author":""}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"
}

response = requests.request("POST", url, headers=headers, params=querystring)

print(response.text)

优化 第一版
调整代码结构,输出结果Json出来,获取需要验证的response.status_code,以及获取结果校验需要用到的results['total']
#!/usr/bin/env python#coding: utf-8'''
unittest merchant backgroud interface
@author: zhang_jin
@version: 1.0
@see:http://www.python-requests.org/en/master/
'''import unittestimport jsonimport tracebackimport requests

url = "http://cpright.xinhua-news.cn/api/match/image/getjson"

querystring = { "category": "image", "offset": "0", "limit": "30", "sourceId": "0", "metaTitle": "", "metaId": "0", "classify": "unclassify", "startTime": "", "endTime": "", "createStart": "", "createEnd": "", "sourceType": "", "isTracking": "true", "metaGroup": "", "companyId": "0", "lastDays": "1", "author": ""
}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"
}#Post接口调用
response = requests.request("POST", url, headers=headers, params=querystring)#对返回结果进行转义成json串
results = json.loads(response.text)#获取http请求的status_codeprint "Http code:",response.status_code#获取结果中的total的值print results['total']#print(response.text)

优化 第二版
接口调用异常处理,增加try,except处理,对于返回response.status_code,返回200进行结果比对,不是200数据异常信息。
#!/usr/bin/env python#coding: utf-8'''
unittest merchant backgroud interface
@author: zhang_jin
@version: 1.0
@see:http://www.python-requests.org/en/master/
'''import jsonimport tracebackimport requests

url = "http://cpright.xinhua-news.cn/api/match/image/getjson"

querystring = { "category": "image", "offset": "0", "limit": "30", "sourceId": "0", "metaTitle": "", "metaId": "0", "classify": "unclassify", "startTime": "", "endTime": "", "createStart": "", "createEnd": "", "sourceType": "", "isTracking": "true", "metaGroup": "", "companyId": "0", "lastDays": "1", "author": ""
}

headers = { 'cache-control': "no-cache", 'postman-token': "e97a99b0-424b-b2a5-7602-22cd50223c15"
}try: #Post接口调用
response = requests.request("POST", url, headers=headers, params=querystring) #对http返回值进行判断,对于200做基本校验 if response.status_code == 200:
results = json.loads(response.text) if results['total'] == 191: print "Success" else: print "Fail" print results['total'] else: #对于http返回非200的code,输出相应的code raise Exception("http error info:%s" %response.status_code)except:
traceback.print_exc()
温馨提示:答案为网友推荐,仅供参考