<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