class RegisterTestCase(unittest.TestCase): """登录和注册接口的验证""" excel = DoExcel(os.path.join(DATA_DIR, "cases.xlsx"), "login®ister") all_cases = excel.read_data() @classmethod def setUpClass(cls): cls.url = config.get("api", "url") cls.request = HttpRequest() cls.mysql = DoMysql() @classmethod def tearDownClass(cls): cls.mysql.close() @data(*all_cases) def test_register(self, case): log.info(f"验证接口:{case.interface},接口url:{case.url}") # 第一步:准备测试数据,替换动态参数 # 替换配置文件中的固定参数 case.data = sub_conf(case.data) # 替换动态生成的参数 if "*phone*" in case.data: # 注册时,手机号码为动态注册的号码 phone = random_phone() case.data = case.data.replace("*phone*", phone) # 第二步:发送请求到接口,获取实际结果 res_code = self.request.http_request(method=case.method, url=self.url + case.url, data=eval(case.data)).json() # 第三步:比对预期结果和实际结果,断言用例是否通过 try: if case.check_sql: case.check_sql = case.check_sql.replace("*phone*", phone) count = self.mysql.find_count(case.check_sql) log.info(f"注册后数据库期望数据条数:1,注册后数据库实际数据条数:{count}") self.assertEqual(1, count) log.info(f"请求期望返回码:{eval(case.expected)},请求实际返回码:{res_code}") self.assertEqual(eval(case.expected), res_code) except AssertionError as e: result = "未通过" log.exception(e) raise e
# 判断是否是执行的加标用例, if case.interface == "加标": loan_id = self.db.find_one( "SELECT Id FROM loan WHERE MemberId='{}' ORDER BY id DESC". format(myconf.get('data', 'memberId'))) # 将添加的标id,保存为临时变量 setattr(ConText, 'loan_id', loan_id[0]) # 第三步 比对预期和实际结果 try: self.assertEqual(str(case.excepted_code), res_code) # 判断是否需要sql校验 if case.check_sql: case.check_sql = data_replace(case.check_sql) # 获取当前用户加标前的标数量 print(case.check_sql) status = self.db.find_one(case.check_sql)[0] self.assertEqual(eval(case.data)["status"], status) except AssertionError as e: # 用例执行未通过 self.excel.write_data(row=case.case_id + 1, column=8, value='未通过') log.info('{}:用例执行未通过'.format(case.title)) log.exception(e) raise e else: # 用例执行通过 self.excel.write_data(row=case.case_id + 1, column=8, value='通过') log.info('{}:用例执行通过'.format(case.title))
def test_shengzhi_public(self,init,init1): #print(case) # 第一步:准备用例数据 # url = myconf.get('url', 'url') + case.url # 读取配置文件和Excel中的url地址进行拼接 # url = myconf.get('url', 'url') #url = case.url url = init[3][1] # 替换用例参数 #case.json = data_replace(str(case.json)) json = data_replace(str(init[4][1])) # if case.interface == '加密接口': # case.json = Encryption_Interface().encryption_interface(case,case.json) # sign = BaseFuntest.get_md5sheng(eval(case.json)) # log.info('签名是:{}'.format(sign)) # case.json = str(case.json).replace('\'', '\"') # j = json.loads(case.json) # j['sign'] = sign # log.info('转换为json的数据{}'.format(j)) # data = eval(case.json)['data'] # datastr = str(data).replace('\'', '\"') # dataspace = str(datastr).replace(' ', '') # log.info('data是:{}'.format(dataspace)) # pc = aes.PrpCrypt('C9C9F54F74BD35DE5242885762E99E8E') # 初始化密钥 # e = pc.encrypt(dataspace) # 加密 # print("加密:", e) # j['data']=e # print('j是{}'.format(j)) # k = str(j).replace('data','encrypt_data') # l = str(k).replace('\'', '\"') # case.json = l # if case.interface == '获取token': json,pc = Access_Token().access_token(json,url) # sign = BaseFuntest.get_md5sheng(eval(case.json)) # log.info('签名是:{}'.format(sign)) # case.json = str(case.json).replace('\'', '\"') # j = json.loads(case.json) # j['sign'] = sign # log.info('转换为json的数据{}'.format(j)) # data = eval(case.json)['data'] # datastr = str(data).replace('\'', '\"') # dataspace = str(datastr).replace(' ', '') # log.info('data是:{}'.format(dataspace)) # pc = aes.PrpCrypt('C9C9F54F74BD35DE5242885762E99E8E') # 初始化密钥 # e = pc.encrypt(dataspace) # 加密 # print("加密:", e) # j['data']=e # print('j是{}'.format(j)) # k = str(j).replace('data','encrypt_data') # l = str(k).replace('\'', '\"') # case.json = l # log.info('请求的参数是:{}'.format(case.json)) # # 第二步 发送请求,获取结果 # log.info('正在请求地址{}'.format(url)) response = init1.request(method=init[2][1], url=url, json=eval(json)) res = response.json() log.info('返回的结果是:{}'.format(res)) datas_encrypt = res['encrypt_data'] log.info("datas_encrypt是:{}".format(datas_encrypt)) d = pc.decrypt(datas_encrypt) # 解密 bianma_d = d.encode().split(b'\x08\x08\x08\x08\x08\x08\x08\x08') bianma_d_str = str(bianma_d) bianma_d_str_de = bianma_d_str.replace("[b'","") bianma_d_str_de_de = bianma_d_str_de.replace("', b'']","") bianma_d_str_de_de_de = eval(bianma_d_str_de_de) access_token = bianma_d_str_de_de_de['access_token'] log.info("access_token:{}".format(access_token)) # 将提取接口返回数据,保存为临时变量 setattr(ConText, 'access_token', access_token) # json = eval(case.json) # log.info('请求的参数是:{}'.format(str(case.json))) # # 第二步 发送请求,获取结果 # log.info('正在请求地址{}'.format(url)) # response = self.http.request(method=case.method, url=url, json=eval(case.json)) # res = response.json() # log.info('返回的结果是:{}'.format(res)) res_code = res['flag'] # 根据接口文档获取出来的是str格式 # 第三步 比对预期结果和实际结果 try: # self.assertEqual(str(init[5][1]), res_code) assert str(init[5][1]) == res_code,"实际结果与预期不符合" except AssertionError as e: # 用例执行未通过 # self.assertNotEqual(str(case.excepted), res_code) # self.excel.write_data(row=case.case_id + 1, column=8, value='未通过') log.info('{}:用例执行未通过'.format(init[0][1])) log.info('请求的地址:{}'.format(url)) log.info('请求的参数是:{}'.format(json)) log.info('返回的结果是:{}'.format(res)) # 执行不通过返回对应结果到日志 log.exception(e) raise e else: # self.excel.write_data(row=case.case_id + 1, column=8, value='通过') log.info('{}:用例执行通过'.format(init[0][1]))