我们在做自动话测试时,一般情况下我们需要查看allure报告,需要allure启动一个服务,但是allure报告有个缺点就是报告不会自动刷新,你你能看到的页面报告就是第一次启动打开的报告,如果想要看到最新的服务报告,需要重新启动一下服务
那么怎么样才能去看到我们想要的报告动态刷新呢?这里我用了python杀掉进程和重启进程完成的
主要步骤有
1:查询allure进程
2:杀掉allure进程
3:启动allure进程
最重要的是用Jenkins去调用python脚本,这样就可以做持续进程了,具体处理看代码
import json import signal import os import requests class RunCmd: def __init__(self): self.netstat = 'netstat -anp | grep 8224'#查询端口进程是否存在 self.pytest_report = 'python3 -m pytest -sv /usr/local/project/jdWebProject/test.py --alluredir allure-temp'#生成allure报告数据文件 self.allure_report = 'allure generate -c -o allure-report allure-temp'#生成allure报告页面文件 self.allure_server = 'allure serve /usr/local/project/jdWebProject/allure-temp -p 8224'#启动报告服务 #查询用例执行成功条数 def dos_result(self): self.r = os.popen(self.pytest_report) count = 0 for line in self.r.readlines(): if line.count("PASSED"): count += 1 print(count) return count # 总用例数2 # 成功用例数count #杀掉进程 def kill_allure_pid(self): self.r = os.popen(self.netstat) pid_list = [] for lin in self.r.readlines(): list_temp = lin.split()#用空切割成列表 if list_temp[6] not in pid_list: # pid_list.append(int(list[6])) pid_num = list_temp[6].split('/') pid_list.append(int(pid_num[0])) if len(pid_list): os.kill(pid_list[0], signal.SIGINT) return True def create_allure_report(self): # success_nun = self.dos_result() self.r = os.popen(self.allure_report) for line in self.r.readlines(): print(line) pass count = 0 if line.count('successfully'): count += 1 print('执行下一个', count) break self.kill_allure_pid() def start_allure_server(self): # self.create_allure_report() self.r = os.popen(self.allure_server) def start_allure_server_send_email(self): import smtplib from email.mime.text import MIMEText # 设置服务器所需信息 # 阿里邮箱服务器地址 mail_host = 'smtp.mxhichina.com' # 阿里邮箱用户名 mail_user = '[email protected]' # 密码(部分邮箱为授权码) mail_pass = 'ertrtrtrnesourcrtrtrtrrtrte@1' # 邮件发送方邮箱地址 sender = '[email protected]' # 邮件接受方邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发 receivers = ['[email protected]', '[email protected]'] # 设置email信息 # 邮件内容设置 success_nun = self.dos_result() # self.create_allure_report() # 杀掉allure进程 self.kill_allure_pid() self.start_allure_server() content = "总用例数:-----2\n成功用例数:-----"+str(success_nun)+"\n详细内容:http://192.168.1.147:8224/index.html" message = MIMEText(content, 'plain', 'utf-8') # 邮件主题 message['Subject'] = '节度使UI自动化测试报告' # 发送方信息 message['From'] = sender # 接受方信息 # message['To'] = receivers[0] message['To'] = ','.join(receivers) # 登录并发送邮件 try: # smtpObj = smtp_lib.SMTP() smtp_obj = smtplib.SMTP(mail_host, 25) # 连接到服务器 # smtpObj.connect(mail_host, 25) # server = smtp_lib.SMTP(mail_host, 25) # 登录到服务器 smtp_obj.login(mail_user, mail_pass) # 发送 smtp_obj.sendmail(sender, receivers, message.as_string()) # 退出 smtp_obj.quit() print('success') except smtplib.SMTPException as e: print('error', e) # 打印错误 #信息发送企业微信 def start_allure_server_send_robot(self, webhook_url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e0866445-084e-4270-8948-b568efa7fd6f'): #获取到成功用例数 success_nun = self.dos_result() # self.create_allure_report() # 杀掉allure进程 self.kill_allure_pid() self.start_allure_server() content = "总用例数:-----2\n成功用例数:-----" + str(success_nun) + "\n详细内容:http://192.168.1.147:8224/index.html" data = {"msgtype": "markdown", "markdown": {"content": content}} r = requests.post(url=webhook_url, data=json.dumps(data, ensure_ascii=False).encode('utf-8')) return r.text, r.status_code t = RunCmd() t.start_allure_server_send_email()
标签:count,pythn,list,self,pid,allure,report,邮件 From: https://www.cnblogs.com/weimeizhizuo/p/18430722