def __init__(self): #千万要注意init容易写成int,会报错,找不到定义的实例变量 # host = "test.lemonban.com" # user = "******" # password = "******" #这样写死了 万一有两个或多个数据库环境,就需要改代码。 # self.mysql=pymysql.connect(host,user,password,port=3306) conf=config.ReadConfig() #读取响应配置中的数据库设置,可以自由更换数据库环境。 host =conf.get('DB','host') user = conf.get('DB', 'user') password = conf.get('DB', 'password') port =conf.getint('DB','port') self.mysql = pymysql.connect(host=host,user=user,password=password, port=port) #建立连接
def __init__(self): read_config = config.ReadConfig(contants.mysql_file) host = read_config.get("mysql", "host") user = read_config.get("mysql", "user") password = read_config.get("mysql", "password") port = read_config.getInt("mysql", "port") # 1.连接数据库 self.mysql = pymysql.connect(host=host, user=user, password=password, port=port) # 2.新建一个查询页面 # self.cursor = self.mysql.cursor() self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) # 单个结果集返回字典,对个结果集返回的是列表,每个元素是字典格式
def get_cases(self): max_row=self.sheet.max_row cases = [] for i in range(2,max_row+1): case=Case() case.case_id = self.sheet.cell(row=i,column=1).value case.title = self.sheet.cell(row=i,column=2).value case.url = url=config.ReadConfig().get('api','pre_url')+self.sheet.cell(row=i, column=3).value case.data = self.sheet.cell(row=i, column=4).value case.method = self.sheet.cell(row=i, column=5).value case.expected = self.sheet.cell(row=i, column=6).value case.sql = self.sheet.cell(row=i,column=9).value cases.append(case)
def replace(data): #print('替换前',data) p="#(.*?)#" #正则表达式 while re.search(p,data): g = re.search(p,data).group(1) #print(g) try: conf = config.ReadConfig().get('data',g) except configparser.NoOptionError as e: conf = str(Context.loan_id) print(conf) # raise e #print(conf) data = re.sub(p,conf,data,count=1) #print('替换后',data) return data
def test_recharge(self, case): conf = config.ReadConfig() pre_url = conf.get('API', 'pre_url') #print(pre_url) resp = self.req.request_method(case.method, pre_url + case.url, case.data) # 返回请求结果 logger.info("case.expected:{}".format(case.expected)) try: self.assertEqual(case.expected, resp.json()['code'], "recharge error") self.do_excel.write_data('recharge', case.case_id + 1, resp.text, 'PASS') logger.info("这是第{}条用例测试结果:PASS".format(case.case_id)) except AssertionError as e: self.do_excel.write_data('recharge', case.case_id + 1, resp.text, 'FAIL') logger.error("这是第{}条用例测试结果:FAIL".format(case.case_id)) # print('case.expected:',type(case.expected)) # print("resp.json()['code']",type(resp.json()['code'])) raise e
def test_invest(self, case): conf = config.ReadConfig() pre_url = conf.get('API', 'pre_url') print(pre_url) #查找参数化的测试数据,动态替换.使用context类. case.data_new = context.replace_new(case.data) #使用封装好的request类来完成请求 resp = self.req.request_method(case.method, pre_url + case.url, case.data_new) # 返回请求结果 print("case.expected:{}".format(case.expected)) try: self.assertEqual(case.expected, resp.json()['code'], "invest error") self.do_excel.write_data('invest', case.case_id + 1, resp.text, 'PASS') #判断是否加标成功,如果加标成功就去查找加标的标id,然后获取loan_id if resp.json()['msg'] == '加标成功': loan_member_id = getattr(context.Context, 'loan_member_id') sql="select id from future.loan where memberId= '{0}'" \ " order by CreateTime desc limit 1".format(loan_member_id) loan_id = self.mysql.fetch_one(sql)[0] setattr(context.Context, 'loan_id', str(loan_id)) # 转化为str,后续正则只处理字符串 # if resp.json()['msg'] =='竞标成功': # leaveamount = self.get_amount() # try: # self.assertEqual(str(self.setUp().amount),str(leaveamount+100),'amount error') # except AssertionError as e: # raise e except AssertionError as e: self.do_excel.write_data('invest', case.case_id + 1, resp.text, 'FAIL') print('case.expected:', type(case.expected)) print("resp.json()['code']", type(resp.json()['code'])) raise e
def replace(data): p = '#(.*?)#' # 正则表达式 while re.search(p, data): print(data) m = re.search(p, data) # 从任意位置开始找,找第一个就返回Match object, 如果没有找None g = m.group(1) # 拿到参数化的key # 根据key获取配置文件对应的值 try: v = config.ReadConfig(contants.global_file).get("data", g) print(v) except configparser.NoOptionError as e: # 如果配置文件找不到的话,就去Context里面找 if hasattr(Context, g): # 如果Context中有属性g,则执行下面代码 v = getattr(Context, g) # 获取类属性g的值 print(v) else: print("找不到参数化的值!") raise e # 将对应的值替换到data中 #注意:替换的时候,一定是字符串替换,不支持数字类型 data = re.sub(p, v, data, count=1) return data
def setUpClass(cls): cls.http_request = HttpRequest() cls.mysql = do_mysql.DoMySQL() cls.config = config.ReadConfig()
def setUpClass(cls): cls.http_request = HttpRequest() cls.config = config.ReadConfig()
# p = '\$\{(.*?)}' #定义正则表达式 # while re.search(p, s) is not None: #一旦找不到匹配的,循环停止 # m = re.search(p, s) #按照p去字符串s中匹配,找到第一个,返回对象m # g = m.group(1) #提取字符串 # value = d[g] # 通过提取的字符串去字典d中查找key # s = re.sub(p, value, s, count=1) #替换 # return s # # # d = {"admin_user": '******', "admin_pwd": '123456'} # s = replace(s, d) # print(s) # 方法二 使用反射来进行替换参数的值 conf = config.ReadConfig() class Context: admin_user = conf.get('DATA', 'admin_user') admin_pwd = conf.get('DATA', 'admin_pwd') loan_member_id = conf.get('DATA', 'loan_member_id') normal_user = conf.get('DATA', 'normal_user') normal_pwd = conf.get('DATA', 'normal_pwd') normal_member_id = conf.get('DATA', 'normal_member_id') def replace_new(s): p = '\$\{(.*?)}' # 定义正则表达式