class DependentData: def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data = GetData() #通过case_id去获取该case_id的整行数据 def get_case_row_data(self): row_data = self.opera_excel.get_row_data_by_caseid(self.case_id) return row_data #执行依赖测试,获取结果 def run_dependent(self): run_method = RunMethod() row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_from_json(row_num) headers = self.data.is_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_url(row_num) res = run_method.run_main(method, url, request_data, headers) return json.loads(res) #根据依赖的key去获取执行以来测试case的响应,最后返回 def get_data_by_key(self, row): dependKey = self.data.get_dependKey(row) resData = self.run_dependent() json_exe = parse(dependKey) madle = json_exe.find(resData) return [math.value for math in madle][0]
def run_dependdent(self): row_num = OperationExcel().get_row_num(self.case_id) url = Get_Token().get_url( client_type=GetData().get_client_type(row_num), api=GetData().get_api(row_num)) method = GetData().get_request_method(row_num) data = GetData().get_data(row_num) data = json.loads(data) header = Get_Token().get_header(row_num) res = RunMethod().run_main(method, url, header, data) return res.json()
class TestCommon: data = GetData().get_excel_data() print(data) @pytest.mark.parametrize( "mokuai, case_name, url, data_method, data, result_method, result", data) def test_excel_case(self, mokuai, case_name, url, data_method, data, result_method, result): # print(mokuai, case_name, url, data_method, data, result_method, result) # 应该再封装一个base, case层调用base的方法即可 allure.description(mokuai + " " + case_name) if data_method == 'get': response = requests.get(url).json() elif data_method == 'post': # 格式化请求数据, 发送post请求 params = data response = requests.post(url, data=params) else: raise TypeError("暂不支持此类型请求") if result_method == '包含': assert result in response elif result_method == '等于': assert result == response
def get_data_for_key(self, row): depend_data = GetData().get_depend_key(row) respond_data = self.run_dependdent() json_exe = parse(depend_data) madle = json_exe.find(respond_data) try: print('获取依赖参数成功:', [math.value for math in madle][0]) return [math.value for math in madle][0] except IndexError: print('获取依赖参数失败', respond_data['message'])
def start_interface_html_http(): starttime = datetime.datetime.now() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basdir = os.path.abspath(os.path.dirname(__file__)) rows_count = GetData().get_case_lines() run = RunTest() listid = [] listname = [] listfangshi = [] listurl ,listconeent,listqiwang,listrelust,list_exption,list_json=[],[],[],[],[],[] for i in range(1, rows_count): result = run.go_on_run(i) if result: listid.append(str(result[0])) listname.append(result[1]) listfangshi.append(result[2]) listurl.append(result[3]) listconeent.append(result[4]) listqiwang.append(result[5]) if result[5] in result[6]: listrelust.append('pass') else: listrelust.append('fail') list_exption.append((result[5])) list_json.append(result[6]) listkey = [i for i in range(80)] list_weizhi = 1 list_pass = 4 list_fail = 5 filepath = os.path.join(basdir + '\\report\\result.html') if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) endtime = datetime.datetime.now() createHtml(titles=u'http接口自动化测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=list_pass, fail=list_fail, id=listid, name=listname, key=listkey, coneent=listconeent, url=listurl, meth=listfangshi, yuqi=listqiwang, json=list_json, relusts=listrelust, weizhi=list_weizhi, exceptions=list_exption) SendEmail().send_main()
class DependData(): def __init__(self,caseid): self.caseid=caseid self.method=Method() self.getdata=GetData() self.excel=Excel() # 获取依赖的响应数据 def get_response_data(self): #caseid = self.get_case_depend(row) row_num = self.excel.get_case_row(self.caseid) url = self.getdata.get_url(row_num) request_method = self.getdata.get_request_method(row_num) request_data = self.getdata.get_request_data(row_num) response_data = self.method.main(request_method, url, request_data) return response_data #获取依赖字段的值 def get_data_value(self,row): depend_data=self.getdata.get_data_depend(row) response_data=self.get_response_data() json_exe=parse(depend_data) madle=json_exe.find(response_data) return [math.value for math in madle][0]
def get_header(self, row): client_type = GetData().get_client_type(row) print(client_type) if not headers.__contains__(client_type): self.save_token(client_type) return headers[client_type]
def __init__(self,caseid): self.caseid=caseid self.method=Method() self.getdata=GetData() self.excel=Excel()
def __init__(self, case_id): self.case_id = case_id self.opera_excel = OperationExcel() self.data = GetData()
def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil()
class RunTest: def __init__(self): self.run_method = RunMethod() self.data = GetData() self.com_util = CommonUtil() def go_on_run(self): pass_count = [] fail_count = [] #10行就,返回10, rows_count = self.data.get_lines() #第0行是头信息,从1行开始 for i in range(1, rows_count): is_run = self.data.get_is_run(i) if is_run: url = self.data.get_url(i) method = self.data.get_request_method(i) requestData = self.data.get_data(i) headers = self.data.is_header(i) expect = self.data.get_expect(i) depend_case = self.data.is_depend(i) if depend_case != None: dependData = DependentData() #获取依赖响应数据 dependResData = dependData.get_data_by_key(i) #获取依赖的field dependField = self.data.get_dependField(i) requestData[dependField] = dependResData res = self.run_method.run_main(method, url, requestData, headers) print(type(res)) if self.com_util.is_contain(expect, res): print("=测试Pass="******"=测试Fail=") self.data.write_result(i, res) fail_count.append(i) sendMail = SendEmail() sendMail.send_main(pass_count, fail_count)
def __init__(self): self.getdata = GetData() self.method = Method() self.content = CommContent() self.logger = Logger().getlog()
class Run(): def __init__(self): self.getdata = GetData() self.method = Method() self.content = CommContent() self.logger = Logger().getlog() def go_on_run(self): cases_count = self.getdata.get_case_lines() for i in range(1, cases_count): name = self.getdata.get_name(i) request_url = self.getdata.get_url(i) request_method = self.getdata.get_request_method(i) request_data = self.getdata.get_request_data(i) is_run = self.getdata.get_is_run(i) expect = self.getdata.get_expect(i) case_depend = self.getdata.get_case_depend(i) if is_run: # print(request_url,request_data,request_method) if case_depend: key_depend = self.getdata.get_data_key_depend(i) dependdata = DependData(case_depend) request_data[key_depend] = dependdata.get_data_value(i) self.logger.info('-----%s-----' % name) self.logger.info('请求url地址:{}'.format(request_url)) self.logger.info('请求参数:%s' % request_data) res = self.method.main(request_method, request_url, request_data) self.logger.info('响应结果:%s' % res) is_content = self.content.content(expect, res) if is_content: self.getdata.write_result(i, 'pass') self.logger.info('通过') else: self.getdata.write_result(i, 'fail') self.getdata.write_response_data(i, res) self.logger.info('失败') return res
def go_on_run(self, i): #运行总函数 # rows_count = GetData().get_case_lines() # for i in range(1,rows_count): is_run = GetData().get_is_run(i) if is_run: url = Get_Token().get_url(client_type=GetData().get_client_type(i), api=GetData().get_api(i)) method = GetData().get_request_method(i) case_name = GetData().get_case_name(i) data = GetData().get_data(i) # print(url,data,method) data = json.loads(data) header = Get_Token().get_header(i) expect = GetData().get_expect_data(i) depend_case = GetData().is_depend(i) if depend_case != None: #获取响应数据 self.depend = DependdentData(depend_case) depend_response_data = self.depend.get_data_for_key(i) #获取依赖的key depend_key = GetData().get_depend_field(int(depend_case)) data[depend_key] = depend_response_data RunMethod().run_main(method, url, header, data) res = RunMethod().run_main(method, url, header, data) if CommonUtil().is_content(expect, res.text): GetData().write_result(i, '测试通过') self.pass_count.append(i) else: GetData().write_result(i, res.text) self.fail_count.append(i) return [i, case_name, method, url, data, expect, res.text]
data = GetData().get_data(i) # print(url,data,method) data = json.loads(data) header = Get_Token().get_header(i) expect = GetData().get_expect_data(i) depend_case = GetData().is_depend(i) if depend_case != None: #获取响应数据 self.depend = DependdentData(depend_case) depend_response_data = self.depend.get_data_for_key(i) #获取依赖的key depend_key = GetData().get_depend_field(int(depend_case)) data[depend_key] = depend_response_data RunMethod().run_main(method, url, header, data) res = RunMethod().run_main(method, url, header, data) if CommonUtil().is_content(expect, res.text): GetData().write_result(i, '测试通过') self.pass_count.append(i) else: GetData().write_result(i, res.text) self.fail_count.append(i) return [i, case_name, method, url, data, expect, res.text] if __name__ == '__main__': run = RunTest() rows_count = GetData().get_case_lines() for i in range(1, rows_count): run.go_on_run(i)
# 数据驱动测试,首先需要安装DDT包 from ddt import ddt, data, unpack from common.get_data import GetData import unittest,os path1 = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'data1.txt') test_data = GetData().txt_data(path1) print(test_data) # 使用前必须先声明 @ddt class MyTest(unittest.TestCase): def setUp(self): print("test start...") def tearDown(self): print("test over...") @data('aa', 'bb') # 单个参数分2次运行用例 def test_1(self, s): print("test1%s" % s) @data((1, 1), (2, 2)) # 多个参数必须要拆包,不然会作为一个参数 @unpack def test_2(self, x, y): print("test2%d" % (x+y)) @data(*test_data) # data的数据必须是元祖,加*是把数据变成元祖类型 test_data 可以是函数*test_data()返回值 @unpack def test_3(self, a, b, c): print(a)