增加邮件发送(带报告附件)

This commit is contained in:
zy7y 2021-03-05 15:55:26 +08:00
parent 68cb02439f
commit c40a5cc46c
3 changed files with 62 additions and 26 deletions

View File

@ -30,8 +30,10 @@ email:
host: smtp.163.com
contents: 解压apiAutoReport.zip(接口测试报告)后请使用已安装Live Server 插件的VsCode打开解压目录下的index.html查看报告
# 收件人邮箱
addressees: ["收件人邮箱1","收件人邮箱2","收件人邮箱3"]
addressees: ["收件人邮箱1", "收件人邮箱2"]
title: 接口自动化测试报告(见附件)
# 附件
enclosures: report.zip
# 数据库校验- mysql
database:

19
run.py
View File

@ -16,15 +16,16 @@ import shutil
from test.conftest import pytest
from tools import logger
from tools.read_file import ReadFile
from tools.send_email import EmailServe
report = ReadFile.read_config('$.file_path.report')
logfile = ReadFile.read_config('$.file_path.log')
file_path = ReadFile.read_config('$.file_path')
email = ReadFile.read_config('$.email')
def run():
if os.path.exists('report/'):
shutil.rmtree(path='report/')
logger.add(logfile, enqueue=True, encoding='utf-8')
logger.add(file_path['log'], enqueue=True, encoding='utf-8')
logger.info("""
_ _ _ _____ _
__ _ _ __ (_) / \ _ _| |_ __|_ _|__ ___| |_
@ -34,16 +35,20 @@ def run():
|_|
Starting ... ... ...
""")
pytest.main(args=['test/test_api.py', f'--alluredir={report}/data'])
pytest.main(args=['test/test_api.py', f'--alluredir={file_path["report"]}/data'])
# 自动以服务形式打开报告
# os.system(f'allure serve {report}/data')
# 本地生成报告
os.system(f'allure generate {report}/data -o {report}/html --clean')
os.system(f'allure generate {file_path["report"]}/data -o {file_path["report"]}/html --clean')
logger.success('报告已生成')
# 发送邮件带附件报告
EmailServe.send_email(email, file_path['report'])
# 删除本地附件
os.remove(email['enclosures'])
if __name__ == '__main__':
run()

View File

@ -9,9 +9,32 @@
"""
import yagmail
from tools import logger
import zipfile
import os
def send_email(setting):
class EmailServe:
@staticmethod
def zip_report(file_path: str, out_path: str):
"""
压缩指定文件夹
:param file_path: 目标文件夹路径
:param out_path: 压缩文件保存路径+xxxx.zip
:return:
"""
file_path = f"{file_path}/html"
zip = zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(file_path):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(file_path, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
@staticmethod
def send_email(setting: dict, file_path):
"""
入参一个字典
:param user: 发件人邮箱
@ -21,11 +44,17 @@ def send_email(setting):
:param addressees: 收件人列表
:param title: 邮件标题
:param enclosures: 附件列表
:param file_path: 需要压缩的文件夹
:return:
"""
EmailServe.zip_report(file_path=file_path, out_path=setting['enclosures'])
yag = yagmail.SMTP(setting['user'], setting['password'], setting['host'])
# 发送邮件
yag.send(setting['addressees'], setting['title'], setting['contents'])
yag.send(setting['addressees'], setting['title'], setting['contents'], setting['enclosures'])
# 关闭服务
yag.close()
logger.info("邮件发送成功!")
# if __name__ == '__main__':
# EmailServe.zip_report('../report/html', 'report.zip')