class Dbconnect(): '''连接数据库操作''' def __init__(self): self.log = Log() self.config = Config().config(db_path, 'DB', 'db_conf') self.db = connector.connect(**self.config) #建立游标 def db_cursor(self): cursor = self.db.cursor() return cursor def db(self, sql, type): """ 操作数据库 :param sql: sql语句 :param type: 参数为1时查询所有,为其他时只查询一条 :return: 返回查询结果 """ cursor = self.db_cursor() cursor.execute(sql) try: if type == 1: db_result = cursor.fetchall() else: db_result = cursor.fetchone() return db_result except Exception as e: self.log.error(f'查询报错{e}') finally: cursor.close() self.db.close()
class Testcases(): log = Log() @allure.story("登录接口") def test_login(self, lg_params): allure.dynamic.title(lg_params['title']) with allure.step(lg_params['title']): self.log.info(f'请求参数为:{lg_params}') #ticket替换 lg_params = ReplaceValue(lg_params).replace_param( 'TICKET', SaveValue.TICKET) res = Requests().httprequest(lg_params['url'], eval(lg_params['param']), lg_params['method'], eval(lg_params['header'])).json() self.log.info(f'请求结果为:{res}') #保存ticket if jsonpath.jsonpath(res, "$..ticket"): SaveValue.TICKET = res['data']['ticket'] else: pass actually_code = res['code'] try: assert lg_params['excepted_code'] == actually_code except Exception as e: self.log.debug(f"用例{lg_params['title']}未通过: 报错为{e}") raise e
class TestCases(): log = Log() @allure.story("身份认证接口") def test_identify(self, identify_params): with allure.step(identify_params["title"]): self.log.info(f'请求的参数为:{identify_params}') re = Requests().httprequest(identify_params['url'], eval(identify_params['param']), identify_params['method'], eval( identify_params['header'])).json() self.log.info(f'返回参数为:{re}') actually_code = re['code'] try: identify_params['excepted_code'] == actually_code except Exception as e: self.log.debug(f"未通过的用例为{identify_params['title']}") raise e
class TestCase(): log = Log() @allure.story("注册接口") def test_register(self, reg_params): with allure.step(reg_params['title']): self.log.info(f'请求参数为 :{reg_params}') res = Requests().httprequest(reg_params['url'], eval(reg_params['param']), reg_params['method'], eval(reg_params['header'])).json() self.log.info(f'请求结果为:{res}') actually_code = res['code'] try: assert reg_params['excepted_code'] == actually_code if reg_params['module'] == 'register': SaveValue.PHONE.append( eval(reg_params['param'])['regName']) RWTestdatas().write_data(SaveValue.PHONE, reginphone_path) except Exception as e: self.log.debug(f"未通过的用例为:{reg_params['title']}") raise e
class Requests(): '''用于发起http请求''' log=Log() def httprequest(self,url,params,method,header=None): re=requests if method.upper()=='GET': try: result=re.get(url,params,headers=header,timeout=1.5) self.log.info(f'{url}的请求结果为{result}') except Exception as e: self.log.debug(f'请求出错,错误为:{e}') raise e elif method.upper()=='POST': try: result = re.post(url, params, headers=header,timeout=1.5) self.log.info(f'{url}的请求结果为{result}') except Exception as e: self.log.debug(f'请求出错,错误为:{e}') raise e else: result=f'参数错误{method}' self.log.debug(result) return result
class Sendemail(): # 引入日志 log = Log() # 发件人邮箱 user = Config().config(email_path, 'EMAIL', 'user') # 邮箱密码 pwd = Config().config(email_path, 'EMAIL', 'pwd') # 时间戳 now = time.strftime('%y-%m-%d_%H_%M_%S') def send_email(self, email_to, filepath): # MIMEMultipart,分多个部分储存邮件 msg = MIMEMultipart() # 邮件的主题 msg['subject'] = self.now + '接口自动化测试报告' # 发件人邮箱 msg['from'] = self.user # 收件人邮箱 msg['to'] = email_to # 添加正文 part = MIMEText('附件为本次接口自动化测试报告请查收!') msg.attach(part) # 添加附件 part = MIMEApplication(open(filepath, 'rb').read()) # 添加邮件请求头 part.add_header('Content-Disposition', 'attachment', filename=filepath) msg.attach(part) # 连接邮箱服务器,默认端口为25 s = smtplib.SMTP_SSL('smtp.qq.com', timeout=30) # 登录邮箱服务器 s.login(self.user, self.pwd) # 发送邮件 s.sendmail(self.user, email_to, msg.as_string()) self.log.info('邮件发送成功') # 退出 s.close()
def warpper(*args,**kwargs): if not SaveValue.TOKEN: header = {'language': 'zh_CN', 'Content-Type': 'application/x-www-form-urlencoded'} method = 'post' re = Requests() request = partial(re.httprequest, method=method, header=header) ip = Config().config(httpconf_path, 'HTTP', 'ip') log=Log() lgname_list=RWTestdatas().read_yaml(reginphone_path) lgname=random.choice(lgname_list) lg_param= { 'loginName': lgname, 'password': '******', 'csessionid': '1', 'sig': '1', 'token': '1', 'platform': '1'} #调登录接口生成ticket lg_url=ip+'/api/v1/user/login' try: lg_re=request(lg_url,lg_param).json() log.info(f'前置登录的请求结果为{lg_re}') except Exception as e: log.error(f'前置登录请求报错:{e}') raise e ticket=jsonpath.jsonpath(lg_re,'$..ticket')[0] sms_url=ip+'/api/v1/user/sendSms' sms_param={ 'type':'127','msgtype':'1', 'areaCode':'86','phone':'1', 'csessionid':'1','sig':'1', 'token':'1','ticket':ticket} #发送短信验证码 try: sms_re=request(sms_url,sms_param).json() log.info(f'前置发送短信接口请求结果为:{sms_re}') except Exception as e: log.error(f'前置发送短信接口报错为:{e}') raise e token_url=ip+'/api/v1/user/getLoginToken' token_param={'code':'123456','ticket': ticket} try: token_re = request(token_url, token_param).json() log.info(f'前置生成token的请求结果为:{token_re}') except Exception as e: log.error(f'前置生成token的请求报错为:{e}') raise e token=jsonpath.jsonpath(token_re,'$..data')[0] SaveValue.TOKEN=token func(*args,**kwargs) return func(*args,**kwargs)
# coning = utf-8 __author__ = 'Aimee' from config.readConfig import ReadConfig import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import time from config.project_path import report_path, data_path, log_path from common.loger import Log log = Log() now_time = time.strftime('%Y-%m-%d %H:%M:%S') #获取配置信息 info = ReadConfig() email_host = info.get_email('mail_host') send_user = info.get_email('mail_send') password = info.get_email('mail_sender_password') sub = info.get_email('subject') user = "******" + "<" + send_user + ">" receivers = info.get_email('receiver').split(';') filepath = [report_path, data_path, log_path] class sendEmail(): def send_mail(self, receivers, filepath): message = MIMEMultipart() message['Subject'] = sub message['From'] = user message['To'] = ','.join(receivers) #邮件正文
def __init__(self): self.log = Log() self.config = Config().config(db_path, 'DB', 'db_conf') self.db = connector.connect(**self.config)