def run_dependent(self): run_method = RunMethod() row_num = self.opera_excel.get_row_num(self.case_id) request_data = self.data.get_data_for_json(row_num) header = self.data.is_header(row_num) method = self.data.get_request_method(row_num) url = self.data.get_request_url(row_num) res = run_method.run_main(method, url, request_data, header) return res
class RunTest: def __init__(self, sheet_id): self.sheet_id = sheet_id self.run_method = RunMethod() # 传递 sheet_id self.data = GetData(self.sheet_id) self.com_util = CommonUtil() ''' # 获取excel行数,也就是case条数 self.rows_count = self.data.get_case_lines() ''' # 程序执行的 def go_on_run(self, i): pass_count = [] fail_count = [] # 改成手动单个单个去执行每行 is_run = self.data.get_is_run(i) if is_run: url = self.data.get_request_url(i) method = self.data.get_request_method(i) header = self.data.is_header(i) request_data = self.data.get_data_for_json(i) # 通过关键字调用 # # request_data = self.data.get_request_data(i) expect = self.data.get_expect_data(i) # expect = self.data.get_expcet_data_for_sql(i) # 获取依赖case_id depend_case = self.data.is_depend(i) if depend_case != None: # 传case_id的值 self.depend_data = DependentData(depend_case, self.sheet_id) # 获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key字段 depend_key = self.data.get_depend_field(i) request_data[depend_key] = depend_response_data res = self.run_method.run_main(method, url, request_data, header) # 转str类型打印 jres = json.dumps(res, ensure_ascii=False, sort_keys=True, indent=2) if self.com_util.is_contain(expect, jres): self.data.write_result(i, 'PASS') pass_count.append(i) else: self.data.write_result(i, jres) fail_count.append(i) return expect, jres return str(self.sheet_id), str(self.sheet_id)
class sms(unittest.TestCase): def setUp(self): self.run = RunMethod() with open('{}'.format( os.path.join(os.path.dirname(os.getcwd()), 'Config') + '\conf.yaml'), "r", encoding="utf-8") as r: config = yaml.load(r) # 解析并读写yaml文件 self.app_host = config['app_host'] self.headers = config['app_headers'] def tearDown(self): pass @data({ "phone": "18657738815", "type": 3, "voice": 0 }, { "phone": "18657738816", "type": 1, "voice": 0 }, { "phone": "18657738817", "type": 2, "voice": 0 }, {}) def test_send_sms(self, data): """ 登录页发送短信验证码 :return: """ # time.sleep() host = self.app_host headers = self.headers lujing = '/system/v1/message_code/send' res = self.run.run_main('post', host, lujing, data, headers) # self.assertTrue(res['code'] == 0, msg=res['msg']) miaoshu(url=host + lujing, method='post', data=data, check={'code and msg'}, respons=res)
class RunTest(unittest.TestCase): def __init__(self): unittest.TestCase.__init__(self) xiao_iron = 'E:/xt/xironbardepend/xironbackend/' self.run_method = RunMethod() self.data = GetData(xiao_iron + 'dataconfig/interfacebar1.xlsx', 2) self.send_mai = SendEmail() self.read_int = ReadIni() # 程序执行的 def go_on_run(self): res = None rows_count = self.data.get_case_lines() success_count = 0 fail_count = 0 total_count = 0 for i in range(1, rows_count): is_run = self.data.get_is_run(i) if is_run: url = self.read_int.get_value() + self.data.get_url(i) method = self.data.get_request_method(i) request_data = self.data.get_data_for_json(i) header = self.data.get_header(i) depend_case = self.data.is_depend(i) if depend_case is not None: self.depend_data = DependentData(depend_case) # 获取的依赖响应数据 depend_response_data = self.depend_data.get_data_for_key(i) # 获取依赖的key depend_key = self.data.get_depend_field(i) if depend_key is None: # 如果依赖字段为空,则替换 url 中的{id} url = url.replace('{id}', depend_response_data) else: # 如果依赖字段有值,则更改请求字段对应的 depend_key 的 value为依赖请求的结果 request_data[depend_key] = depend_response_data if method != 'get': request_data = json.dumps(request_data) total_count += 1 res = self.run_method.run_main(method, url, request_data, header) expect_res_str = self.data.get_expect_data(i).__str__() if expect_res_str is not None: expect_res_list = expect_res_str.split('/') all_expect_pass = True for a in expect_res_list: # 断言报错继续执行 try: # self.assertIn(self.data.get_expect_data(i), json.dumps(res), "返回结果不是期望结果") # 因为返回结果中不会有中文,所以将期望值的中文转成 Unicode 的 byte ,为了匹配 assertIn 的参数类型,再转成 utf-8的str self.assertIn(a.encode("unicode_escape").decode('utf-8'), json.dumps(res), "返回结果不是期望结果") print("用例编号:", self.data.get_case_id(i), ",接口自动化测试通过") self.data.write_result(i, 'pass') except AssertionError as e: print("用例编号:", self.data.get_case_id(i), ",接口自动化测试失败!!!\n", "断言异常为:", e) self.data.write_result(i, 'fail' + e.__str__().encode("utf-8").decode('utf-8')) # fail_count += 1 all_expect_pass = False if all_expect_pass: success_count += 1 else: fail_count += 1 else: # 断言报错继续执行 try: self.assertEqual(res.status_code, 204, '该操作不成功,无返回结果') print("用例编号:", self.data.get_case_id(i), ",接口自动化测试通过") self.data.write_result(i, 'pass') success_count += 1 except AssertionError as e: print("用例编号:", self.data.get_case_id(i), ",接口自动化测试失败!!!\n", "断言异常为:", e) self.data.write_result(i, 'fail' + e.__str__()) fail_count += 1 if type(res) is not dict: self.data.write_res_to_pre(i, res.__str__().encode("utf-8").decode('utf-8')) else: # self.data.write_res_to_pre(i, json.dumps(res)) # 回写 excel时,通过json.dumps() 会将中文编译成 Unicode,这里直接处理成 str # 结果回写需要是 json格式,将单引号换成双引号 pre_json = res.__str__().replace('\'', '\"') self.data.write_res_to_pre(i, pre_json) print("本次接口自动化测试运行用例总数:", total_count) print("本次接口自动化测试通过用例数:", success_count) print("本次接口自动化测试失败用例数:", fail_count)
class login(unittest.TestCase): def setUp(self): self.run = RunMethod() # pass def tearDown(self): pass @data( { "mobile": "18657738815", "password": "******" }, { "mobile": "13071863055", "password": "******" }, unpack=False) def test_dealer_login(self, data): ''' 车商登陆:密码 :return:session ''' host = 'http://192.168.31.172:8080' lujing = '/dealer/login' # datas = {"mobile": "18657738815", "password": "******"} headers = { "version": "2.0.0", "Connection": "keep-alive", "Content-Type": "application/json; charset=utf-8" } res = self.run.run_main('post', host, lujing, data=data, headers=headers) self.assertTrue(str(res['code']) == '200', msg='状态不对') self.assertIn('success', res['msg'], msg='a不在b中') print(res) @data(18657738815, 15805811478, 13071863055) def test_cms_login(self, user): """后台登陆:密码 :return:cookie """ url = 'http://192.168.31.172:8082/cms/login' datas = {"username": user, "password": 111111} res = requests.post(url, datas) response = res.json() self.assertTrue(str(response['code']) == '200', msg='状态不对') self.assertIn('success', response['msg'], msg='a不在b中') print(response) @data(["gold", 100], ["diamond", 500]) def test_bless(self, bless_type, award): ''' 领取宝箱 :param bless_type: :param award: :return: ''' print(bless_type) print(award)
class RunTest(unittest.TestCase): def __init__(self): unittest.TestCase.__init__(self) xiao_iron = 'E:/xt/xtcontract/xironbackend/dataconfig/' self.run_method = RunMethod() self.data = GetData(xiao_iron + 'interfacebar1.xlsx', 8) self.send_mai = SendEmail() self.read_int = ReadIni() self.statistic = Dict() self.excel_prop = Dict() # 程序执行的 def go_on_run(self): self.excel_prop.rows_count = self.data.get_case_lines() self.statistic.success_count = 0 self.statistic.fail_count = 0 self.statistic.total_count = 0 for i in range(2, self.excel_prop.rows_count): self.excel_prop.index = i self.excel_prop.is_run = self.data.get_is_run(i) if self.excel_prop.is_run: self.excel_prop.url = self.read_int.get_value( ) + self.data.get_url(i) self.excel_prop.method = self.data.get_request_method(i) self.excel_prop.request_data = self.data.get_data_for_json(i) self.excel_prop.header = self.data.get_header(i) self.excel_prop.case_depend = self.data.get_case_depend(i) if self.excel_prop.case_depend is not None and CommonUtil.is_json( self.excel_prop.case_depend): self.depend_data_json = DependentDataJson(self.excel_prop) self.excel_prop = self.depend_data_json.assemble_excel_prop( ) if self.excel_prop.method != 'get': self.excel_prop.request_data = json.dumps( self.excel_prop.request_data) self.statistic.total_count += 1 res = self.run_method.run_main(self.excel_prop.method, self.excel_prop.url, self.excel_prop.request_data, self.excel_prop.header) self.excel_prop.expect_res_str = self.data.get_expect_data( i).__str__() if self.excel_prop.expect_res_str is not None: expect_res_list = self.excel_prop.expect_res_str.split('/') all_expect_pass = True for expect_res in expect_res_list: # 断言报错继续执行 try: # 因为返回结果中不会有中文,所以将期望值的中文转成 Unicode 的 byte ,为了匹配 assertIn 的参数类型,再转成 utf-8的str self.assertIn( expect_res.encode("unicode_escape").decode( 'utf-8'), json.dumps(res), "返回结果不是期望结果") print("用例编号:", self.data.get_case_id(i), ",接口自动化测试通过") self.data.write_result(i, 'pass') except AssertionError as e: print("用例编号:", self.data.get_case_id(i), ",接口自动化测试失败!!!\n", "断言异常为:", e) self.data.write_result( i, 'fail' + e.__str__().encode("utf-8").decode('utf-8')) # self.statistic.fail_count += 1 all_expect_pass = False if all_expect_pass: self.statistic.success_count += 1 else: self.statistic.fail_count += 1 else: # 断言报错继续执行 try: self.assertEqual(res.status_code, 204, '该操作不成功,无返回结果') print("用例编号:", self.data.get_case_id(i), ",接口自动化测试通过") self.data.write_result(i, 'pass') self.statistic.success_count += 1 except AssertionError as e: print("用例编号:", self.data.get_case_id(i), ",接口自动化测试失败!!!\n", "断言异常为:", e) self.data.write_result(i, 'fail' + e.__str__()) self.statistic.fail_count += 1 if type(res) is not dict: self.data.write_res_to_pre( i, res.__str__().encode("utf-8").decode('utf-8')) else: # 回写 excel时,通过json.dumps() 会将中文编译成 Unicode,这里直接处理成 str # 结果回写需要是 json格式,将单引号换成双引号 pre_json = res.__str__().replace('\'', '\"') self.data.write_res_to_pre(i, pre_json) print("本次接口自动化测试运行用例总数:", self.statistic.total_count) print("本次接口自动化测试通过用例数:", self.statistic.success_count) print("本次接口自动化测试失败用例数:", self.statistic.fail_count)