#coding=utf-8
import sys
import os
import json
from Util.handle_excel import excel_data
from Base.base_request import request
from Util.handle_result import handle_result, get_result_json,handle_result_json
from Util.handle_cookie import get_cookie_value
from Util.handle_header import get_header
from Util.codition_data import get_data
base_path = os.path.dirname(os.getcwd())
sys.path.append(base_path)
class RunMain():
def run_case(self):
rows = excel_data.get_rows()
for i in range(rows):
cookie = None
get_cookie = None
header = None
depend_data = None
data = excel_data.get_rows_value(i+2)
is_run = data[2]
if is_run == ‘yes’:
is_depend = data[3]
data1 = json.loads(data[3])
if is_depend:
’’‘获取依赖数据’’‘
depend_key = data[4]
print(“is_depend=====”,is_depend)
depend_data = get_data(is_depend)
data1[depend_key] = depend_data
method = data[6]
url = data[5]
is_header = data[9]
excepect_method = data[10]
excepect_result = data[11]
cookie_method = data[8]
if cookie_method == ‘yes’:
cookie = get_cookie_value(‘app’)
if cookie_method == ‘write’:
’’‘必须是获取到cookie’’'
get_cookie = {“is_cookie”:“app”}
if is_header == ‘yes’:
header = get_header()
res = request.run_main(method, url, data1, cookie, get_cookie, header)
code = str(res[‘errorCode’])
message = res[‘errorDesc’]
if excepect_method == ‘mec’:
config_message = handle_result(url,code)
if message == config_message:
excel_data.excel_write_data(i+2, 13, “通过”)
else:
excel_data.excel_write_data(i+2, 13, “失败”)
excel_data.excel_write_data(i+2, 14, json.dumps(res))
if excepect_method == ‘errorcode’:
if excepect_result == code:
excel_data.excel_write_data(i+2, 14, “通过”)
else:
excel_data.excel_write_data(i + 2, 13, “失败”)
excel_data.excel_write_data(i + 2, 14, json.dumps(res))
if excepect_method == ‘json’:
if code == 1000:
status_str = 'sucess’
else:
status_str = 'error’
excepect_result = get_result_json(url, status_str)
result = handle_result_json(res, excepect_result)
if result:
excel_data.excel_write_data(i+2, 13, “通过”)
else:
excel_data.excel_write_data(i + 2, 13, “失败”)
excel_data.excel_write_data(i + 2, 14, json.dumps(res))
这里只判断了is_depend为True的情况下,后面有else,那么要是is_depend为False会怎么去处理呢,不是所有的接口都有依赖,我试过登录接口不需要依赖,请求完后将登录接口的token写入到Excel中,然后到了需要依赖的接口再去取Excel中的依赖,但是我取到的一直是上一次的token,不是登录接口写入到Excel中的最新的token,下面是我自己的run文件的代码,请老师给我指导一下
#coding=utf-8
import json
from Base.run_metnod import RunMethod
from Data.get_data import GetData
from Data.dependent_data import DependentData
from Util.common_util import CommonUtil
from Util.operation_header import OperationHeader
from Util.operation_json import OperationJson
from Util.operation_excel import opera_excel
from Base.encryption_decryption import encrypt, decrypt
class RunTest:
def __init__(self):
self.run_method = RunMethod()
self.data = GetData()
self.com_util = CommonUtil()
def go_on_run(self):
'''
程序执行
:return:
'''
pass_count = []
fail_count = []
res = None
# 获取用例数
rows_count = self.data.get_case_lines()
# 第一行索引为0
for i in range(1,rows_count):
is_run = self.data.get_is_run(i)
print('is_run是True还是False:',is_run)
if is_run:
host = self.data.get_request_host(i)
url = self.data.get_request_url(i)
# print('url---->',url)
request_url = url + host
print('request_url===>',request_url)
method = self.data.get_request_method(i)
# print('method---->',method)
# request_data = self.data.get_data_for_json(i)
expect = self.data.get_expect_data(i)
header = self.data.is_header(i)
depend_case = self.data.is_depend(i)
print('46行depend_case---->',depend_case)
if depend_case != None:
# 根据case_id去获取依赖case_id的整条数据
depend_row = opera_excel.get_row_num(depend_case) # depend_row是获取case_id所在的行
depend_key = opera_excel.get_row_value(depend_row)
print('57行,depend_key====>', depend_key)
data = self.data.get_request_data(i)
request_depend_data = data + "&" + depend_key # 加上依赖的token后的请求data
print('有依赖后的data:', request_depend_data)
encrypt_data = encrypt(request_depend_data).text
request_data = {'data': encrypt_data}
print('加密后的请求data是=====>',request_data)
res = self.run_method.run_main(method, request_url, request_data)
r = json.loads(res)
print('r========>', r)
# 根据结果去断言
if expect != None:
if self.com_util.is_contain(expect,res):
self.data.write_result(i, "pass")
pass_count.append(i)
else:
self.data.write_result(i, res)
fail_count.append(i)
else:
print(f"用例ID:case_{i},预期结果不能为空")
else:
request_data = self.data.get_request_data(i)
# print('41行request_data====',request_data)
data = self.data.get_request_data(i)
# print('43行data----》', data)
encrypt_data = encrypt(data).text # 加密请求参数
print('encrypt_data=====>', encrypt_data)
request_data = {'data': encrypt_data}
print('request_data======>', request_data)
res = self.run_method.run_main(method, request_url, request_data)
# print('res的类型是====》',type(res))
r = json.loads(res)
# print('r的类型是====》',type(r))
print('r========>',r)
data_res = r['data']
print('data_res==========>>>>>>',data_res)
decryption_result = decrypt(data_res).json()
print('已解密的返回参数:', decryption_result)
# print('decryption_result的类型是:',type(decryption_result))
self.data.write_result(i, json.dumps(decryption_result))
self.data.write_depend_data(i, json.dumps(decryption_result))
login_token = decryption_result['user_token']
token = '&token=' +login_token
# print('login_token:', token)
depend_user_id = decryption_result['user_id']
user_id = '&user_id=' + depend_user_id
depend = token + user_id
self.data.write_depend_key(i, depend)
# print('获取到的user_id存到依赖key中:', user_id)
print('依赖的key为=======', depend)
print(f"通过用例数:{len(pass_count)}")
print(f"失败用例数:{len(fail_count)}")