python api测试框架 github_我的 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 模型

导购码接口测试

dgm.XXXX

80

[] # 指定需要运行哪些接口

# 第一个层固定预留,只用于登陆接口

1001

登陆

POST

/Login

{"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}]}

{"account":"18576759587","password":"1112111","type":"0"}

user_id # 登陆后返回的userid,token等

0 # 是否有嵌套

1002

厂家主页

GET

/GetFactoryHome?homeId=2

{"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}]}

{}

1 # 0不需要登陆后的参数,1表示需要登陆后的参数

1 # 是否有嵌套层

入口代码 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 页面

页面比较丑没有去美化

9f064875a18505fe59cdf374c1b23937.png

后续优化

接口加密

美化 UI

期望值从其他接口查询

其他优化

github 源码

后续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值