def __init__(self): # 创建一个连接对象 self.conn = pymysql.connect(host=conf.get("db", "host"), port=conf.getint("db", "port"), user=conf.get("db", "user"), password=conf.get("db", "pwd"), charset=conf.get("db", "charset"), cursorclass=pymysql.cursors.DictCursor) # 创建一个游标 self.cur = self.conn.cursor()
def setUpClass(cls) -> None: url = conf.get('env', 'url') + '/user/login/' method = 'post' data = { 'username': conf.get('test_data', 'user'), 'password': conf.get('test_data', 'pwd') } response = cls.request.send(url=url, method=method, json=data) res = response.json() CaseData.token = 'JWT ' + jsonpath.jsonpath(res, '$.token')[0]
def test_cases(self, case): # 准备测试数据 url = conf.get('env', 'url') + case['url'] method = case['method'] headers = {'Authorization': CaseData.token} CaseData.case_name = random_data() case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 # 获取结果 response = self.request.send(url=url, method=method, headers=headers, json=data) res = response.json() status = response.status_code # 对预期结果和相应结果进行断言 try: self.assertEqual(expected['status'], status) if '成功' not in case['title']: self.assertIn(expected['msg'], str(res.values())) except AssertionError as E: print('预期结果:', expected) print('实际结果:', status, res) self.excel.write_data(row=row, column=8, value='不通过') log.error('{}用例不通过'.format(case['title'])) log.exception(E) raise E else: self.excel.write_data(row=row, column=8, value='通过')
def test_mainflow(self, case): # 准备测试数据 CaseData.username = self.random_user() CaseData.email = self.random_email() CaseData.project_name = random_data() CaseData.interfaces = random_data() CaseData.case_name = random_data() url = conf.get('env', 'url') + replace_data(case['url']) method = case['method'] case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 # 获取结果 if case['interface'] != 'register' or case['interface'] != 'login': headers = {'Authorization': CaseData.token} response = self.request.send(url=url, method=method, headers=headers, json=data) else: response = self.request.send(url=url, method=method, json=data) res = response.json() status = response.status_code if case['interface'] == 'login': CaseData.token = 'JWT ' + jsonpath.jsonpath(res, '$.token')[0] # 对预期结果和相应结果进行断言 try: self.assertEqual(expected['status'], status) except AssertionError as E: print('预期结果:', expected) print('实际结果:', status, res) self.excel.write_data(row=row, column=8, value='不通过') log.error('{}用例不通过'.format(case['title'])) log.exception(E) raise E else: self.excel.write_data(row=row, column=8, value='通过')
def test_regiter(self, case): # 准备测试数据 CaseData.username = self.random_user() CaseData.email = self.random_email() url = conf.get('env', 'url') + replace_data(case['url']) method = case['method'] case['data'] = replace_data(case['data']) data = eval(case['data']) expected = eval(case['expected']) row = case['case_id'] + 1 # 获取结果 response = self.request.send(url=url, method=method, json=data) res = response.json() status = response.status_code # 对预期结果和相应结果进行断言 try: self.assertEqual(expected['status'], status) if case['title'] != '注册成功': if case['method'].lower() == 'get': self.assertEqual(expected['count'], res['count']) else: self.assertIn(expected['msg'], str(res.values())) except AssertionError as E: print('预期结果:', expected) print('实际结果:', status, res) self.excel.write_data(row=row, column=8, value='不通过') log.error('{}用例不通过'.format(case['title'])) log.exception(E) raise E else: self.excel.write_data(row=row, column=8, value='通过')
def replace_data(s): r1 = r'#(.+?)#' while re.search(r1, s): res = re.search(r1, s) key = res.group(1) try: value = conf.get("test_data", key) except Exception: value = getattr(CaseData, key) finally: s = re.sub(r1, value, s, 1) return s
def create_log(): # 创建收集器,设置收集器的等级 mylog = logging.getLogger(conf.get('log', 'name')) # 设置日志收集器的等级 mylog.setLevel(conf.get('log', 'level')) # 创建输出到控制台的渠道,并设置等级 sh = logging.StreamHandler() sh.setLevel(conf.get('log', 'sh_level')) mylog.addHandler(sh) # 设置输出到文件的渠道,并设置等级 filepath = os.path.join(LOGDIR, 'log.log') fh = logging.FileHandler(filename=filepath, encoding='utf8') fh.setLevel(conf.get('log', 'fh_level')) mylog.addHandler(fh) # 设置日志输出格式 fm = logging.Formatter(conf.get('log', 'format')) sh.setFormatter(fm) fh.setFormatter(fm) return mylog
def send_email(filename, title): """ 发送邮件的功能函数 :param filename: 文件的路径 :param title: 邮件的主题 :return: """ # 第一步:连接邮箱的smtp服务器,并登录 smtp = smtplib.SMTP_SSL(host=conf.get('email', 'host'), port=conf.getint('email', 'port')) smtp.login(user=conf.get('email', 'user'), password=conf.get('email', 'pwd')) # 第二步:构建一封邮件 # 1、创建一封多组件的邮件 msg = MIMEMultipart() with open(filename, 'rb') as f: content = f.read() # 2、创建邮件文本内容 text_msg = MIMEText(content, _subtype='html', _charset='utf8') # 3、添加到多组件的邮件中 msg.attach(text_msg) # 4、创建邮件的附件 report_file = MIMEApplication(content) report_file.add_header('content-disposition', 'attachment', filename=filename) # 5、将附件添加到多组件的邮件中 msg.attach(report_file) # 6、编辑邮件信息 msg['Subject'] = title msg['From'] = conf.get('email', 'from_addr') msg['To'] = conf.get('email', 'to_addrs') # 第三步:发送邮箱 smtp.send_message(msg, from_addr=conf.get('email', 'from_addr'), to_addrs=conf.get('email', 'to_addrs'))