1. STest软件测试社区首页
  2. 接口测试

Python接口测试框架

简单介绍

  • Win7 64,python 3,Pycharm. unittest
  • 读取配置文件–读取测试用例–执行测试用例–记录测试结果–生成html结果文件
  • 支持指定接口用例id的测试
  • 考虑到登陆后返回的token,useId给其他接口联合使用的情况
  • 使用html在线生成器生成xml,基于xml管理数据,只要改xml文件,不用改其他代码。
  • 检查点的定义
    • 检查第一层的全字段
    • 如果有嵌套层,检查第一层的全字段后,检查嵌套层的model的type类型

模块类的设计说明

  • Httpbase.py 读取http的域名和端口
  • Config.py http方法的封装,可以支持多协议扩展,get,post
  • Runner_m.py 核心代码。run_case是程序的入口
  • Htmlreport.py 结果生成html文件

xml模型

<root>
<title>导购码接口测试</title>
<host>dgm.XXXX</host>
<port>80</port>
<No>[]</No> # 指定需要运行哪些接口
<InterfaceList> # 第一个层固定预留,只用于登陆接口
<id>1001</id>
<name>登陆</name>
<method>POST</method>
<url>/Login</url>
<hope>{"appStatus":{"errorCode":0,"message":"操作成功"},"content":[{"user_sex":0,"fk_user_city":440300,"user_id":30,"nickname":"18576759587","user_phone":"18576759587","head_picture":"http:\/\/dgm.boweixin.com\/","has_finance":1,"user_state":1}]}</hope>
<params>{"account":"18576759587","password":"1112111","type":"0"}</params>
<login>user_id</login> # 登陆后返回的userid,token
<isList>0</isList> # 是否有嵌套
<!--Version=3.0&UserName=18576759587&IMEI=868157020567821&Password=222222&City=%E6%B7%B1%E5%9C%B3%E5%B8%82&Province=%E5%B9%BF%E4%B8%9C%E7%9C%81&Plat=android&PhoneModel=H60-L02-->
</InterfaceList>
<InterfaceList>
<id>1002</id>
<name>厂家主页</name>
<method>GET</method>
<url>/GetFactoryHome?homeId=2</url>
<hope>{"appStatus":{"errorCode":0,"message":"操作成功"},"content":[{"business_name":"坤达点子","notice_img":"\/product\/20160718184134_321.jpg","user_type":1,"user_id":2,"goods":[{"good_price":45211.0,"good_id":12,"good_name":"艾欧","banner_picture1":"\/product\/20160719165135_8977.png"},{"good_price":199.0,"good_id":14,"good_name":"麒麟瓜1","banner_picture1":"\/product\/20160720102028_5352.jpg"},{"good_price":452.0,"good_id":6,"good_name":"实力产品","banner_picture1":"\/product\/20160718165448_2602.png"},{"good_price":99898.0,"good_id":11,"good_name":"越南芒果","banner_picture1":"\/product\/20160720100057_5877.jpg"}],"shop_img":"\/product\/20160718120144_3196.jpg","head_picture":"http:\/\/dgm.boweixin.com\/\/product\/20160718134528_4744.jpg","notice_id":1}]}</hope>
<params>{}</params>
<login>1</login> # 0不需要登陆后的参数,1表示需要登陆后的参数
<isList>1</isList> # 是否有嵌套层
</InterfaceList>

入口代码

gm = con_api_xml.ret_xml() # 读取xml
hb = con_api_xml.ret_http_base(gm) #读取http参数


#初始化报告
html_report1 = htmlreport.HtmlReport(gm)

# 测试用例()
class TestInterfaceCase(unittest.TestCase):
def __init__(self, testName, hope, index):
super(TestInterfaceCase, self).__init__(testName)
self.hope = hope
self.index = index
def setUp(self):
self.config_http = config.ConfigHttp(hb.host, hb.port)
def function(self):
response = ""
if self.index == 1: # 登陆的接口测试
if gm[self.index]["method"] == "POST":
response = self.config_http.post(go.URL, go.PARAMS)
go.REALLY_RESULT = eval(response)
print(go.REALLY_RESULT)
hope = eval(self.hope)
# temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
temp = check.compare(hope,go.REALLY_RESULT)
if temp:
go.LOGIN_KY = gm[1]["login"]
go.LOGIN_VALUE = go.REALLY_RESULT["content"][0][go.LOGIN_KY]
go.RESULT = 'Pass'
html_report1.success_num = html_report1.success_num + 1
else:
go.RESULT = 'Fail'
html_report1.error_num = html_report1.error_num + 1
else:
if gm[self.index]["login"] != "0":
go.PARAMS[go.LOGIN_KEY] = go.LOGIN_VALUE
if gm[self.index]["method"] == "POST":
response = self.config_http.post(go.URL, go.PARAMS)
if gm[self.index]["method"] == "GET":
response = self.config_http.get(go.URL, go.PARAMS)
print(type(response))
go.REALLY_RESULT = eval(str(response))
hope = eval(self.hope)
# temp = testJson.compareJson(hope, go.REALLY_RESULT, gm[self.index]["isList"])
temp = check.compare(hope,go.REALLY_RESULT, gm[self.index]["isList"])
print(temp)
if temp:
go.RESULT = 'Pass'
html_report1.success_num = html_report1.success_num + 1
# except AssertionError:
else:
go.RESULT = 'Fail'
html_report1.fail_num = html_report1.fail_num + 1
# 获取测试套件
def get_test_suite(index):
test_suite = unittest.TestSuite()
hope = gm[index]["hope"] # 预期值
# print(hope)
test_suite.addTest(TestInterfaceCase("function", hope,index))
return test_suite

# 运行测试用例函数
def run_case(runner):
html_report1.case_total = 0
case_list = hb.No
case_list = eval(case_list) # 把字符串类型的list转换为list
html_report1.case_list = case_list
temp_case = ""
if len(case_list) == False: #判断是否执行指定的用例ID
temp_case = gm
for index in range(1, len(temp_case)):
go.URL = gm[index]['url']
go.PARAMS = gm[index]["params"]
test_suite = get_test_suite(index)
runner.run(test_suite)
# 记录运行结果
gm[index]["result"] = go.RESULT
gm[index]["really_result"] = go.REALLY_RESULT
else:
for i in case_list:
for j in range(1, len(gm)):
if str(i) == gm[j]['id']:
go.URL = gm[j]['url']
go.PARAMS = gm[j]["params"]
test_suite = get_test_suite(j)
runner.run(test_suite)
gm[j]["result"] = go.RESULT
gm[j]["really_result"] = go.REALLY_RESULT
# 运行测试套件
if __name__ == '__main__':
start_time = time.time()
runner = unittest.TextTestRunner()
run_case(runner)
end_time = time.time()
html_report1.time_caculate(end_time - start_time) # 计算测试消耗时间
html_report1.generate_html( r'D:\\app\\auto_http34_test\\report\report.html') # 生成测试报告

检查点的代码

def compare(exJson,factJson,isList=0):
isFlag = True
if exJson.get("appStatus") == factJson.get("appStatus"):
if isList== False: # 如果没有嵌套层
return isFlag
data2 = exJson.get("content")
data3 = factJson.get("content")
for item2 in data2:
for item3 in data3:
keys2 = item2.keys()
keys3 = item3.keys()
if keys2 == keys3: # 如果嵌套层的key完全相等
for key in keys2:
value2 = item2.get(key)
value3 = item3.get(key)
if type(value3)==type(value2):
pass
else:
isFlag = False
break
else:
isFlag = False
break
else:
isFlag = False
print(isFlag)
return isFlag

运行结果用的是pyh拼接的html页面

  • 页面比较丑没有去美化 Python接口测试框架

后续优化

  • 接口加密
  • 美化UI
  • 期望值从其他接口查询
  • 其他优化

github源码

  • 查看接口框架源码
  • 接口生成器

后续

  • 有人说我复制人家的框架来分享,嗯,我贴上原文链接: http://blog.sina.com.cn/s/blog_13cc013b50102vgr7.html
  • 管理员可以删除这篇帖子,我也没有意见。

,


33 个赞
举报

原创文章,作者:测试媛,如若转载,请注明出处:http://www.stest.com

发表评论

电子邮件地址不会被公开。 必填项已用*标注

QR code