forked from DxvLwRYF/apiAutoTest
增加邮件发送(带报告附件)
This commit is contained in:
parent
68cb02439f
commit
c40a5cc46c
|
@ -30,8 +30,10 @@ email:
|
||||||
host: smtp.163.com
|
host: smtp.163.com
|
||||||
contents: 解压apiAutoReport.zip(接口测试报告)后,请使用已安装Live Server 插件的VsCode,打开解压目录下的index.html查看报告
|
contents: 解压apiAutoReport.zip(接口测试报告)后,请使用已安装Live Server 插件的VsCode,打开解压目录下的index.html查看报告
|
||||||
# 收件人邮箱
|
# 收件人邮箱
|
||||||
addressees: ["收件人邮箱1","收件人邮箱2","收件人邮箱3"]
|
addressees: ["收件人邮箱1", "收件人邮箱2"]
|
||||||
title: 接口自动化测试报告(见附件)
|
title: 接口自动化测试报告(见附件)
|
||||||
|
# 附件
|
||||||
|
enclosures: report.zip
|
||||||
|
|
||||||
# 数据库校验- mysql
|
# 数据库校验- mysql
|
||||||
database:
|
database:
|
||||||
|
|
19
run.py
19
run.py
|
@ -16,15 +16,16 @@ import shutil
|
||||||
from test.conftest import pytest
|
from test.conftest import pytest
|
||||||
from tools import logger
|
from tools import logger
|
||||||
from tools.read_file import ReadFile
|
from tools.read_file import ReadFile
|
||||||
|
from tools.send_email import EmailServe
|
||||||
|
|
||||||
report = ReadFile.read_config('$.file_path.report')
|
file_path = ReadFile.read_config('$.file_path')
|
||||||
logfile = ReadFile.read_config('$.file_path.log')
|
email = ReadFile.read_config('$.email')
|
||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
if os.path.exists('report/'):
|
if os.path.exists('report/'):
|
||||||
shutil.rmtree(path='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("""
|
logger.info("""
|
||||||
_ _ _ _____ _
|
_ _ _ _____ _
|
||||||
__ _ _ __ (_) / \ _ _| |_ __|_ _|__ ___| |_
|
__ _ _ __ (_) / \ _ _| |_ __|_ _|__ ___| |_
|
||||||
|
@ -34,16 +35,20 @@ def run():
|
||||||
|_|
|
|_|
|
||||||
Starting ... ... ...
|
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 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('报告已生成')
|
logger.success('报告已生成')
|
||||||
|
|
||||||
|
# 发送邮件带附件报告
|
||||||
|
EmailServe.send_email(email, file_path['report'])
|
||||||
|
|
||||||
|
# 删除本地附件
|
||||||
|
os.remove(email['enclosures'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run()
|
run()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,23 +9,52 @@
|
||||||
"""
|
"""
|
||||||
import yagmail
|
import yagmail
|
||||||
from tools import logger
|
from tools import logger
|
||||||
|
import zipfile
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
def send_email(setting):
|
class EmailServe:
|
||||||
"""
|
|
||||||
入参一个字典
|
@staticmethod
|
||||||
:param user: 发件人邮箱
|
def zip_report(file_path: str, out_path: str):
|
||||||
:param password: 邮箱授权码
|
"""
|
||||||
:param host: 发件人使用的邮箱服务 例如:smtp.163.com
|
压缩指定文件夹
|
||||||
:param contents: 内容
|
:param file_path: 目标文件夹路径
|
||||||
:param addressees: 收件人列表
|
:param out_path: 压缩文件保存路径+xxxx.zip
|
||||||
:param title: 邮件标题
|
:return: 无
|
||||||
:param enclosures: 附件列表
|
"""
|
||||||
:return:
|
file_path = f"{file_path}/html"
|
||||||
"""
|
zip = zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED)
|
||||||
yag = yagmail.SMTP(setting['user'], setting['password'], setting['host'])
|
for path, dirnames, filenames in os.walk(file_path):
|
||||||
# 发送邮件
|
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
|
||||||
yag.send(setting['addressees'], setting['title'], setting['contents'])
|
fpath = path.replace(file_path, '')
|
||||||
# 关闭服务
|
|
||||||
yag.close()
|
for filename in filenames:
|
||||||
logger.info("邮件发送成功!")
|
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
|
||||||
|
zip.close()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def send_email(setting: dict, file_path):
|
||||||
|
"""
|
||||||
|
入参一个字典
|
||||||
|
:param user: 发件人邮箱
|
||||||
|
:param password: 邮箱授权码
|
||||||
|
:param host: 发件人使用的邮箱服务 例如:smtp.163.com
|
||||||
|
:param contents: 内容
|
||||||
|
: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'], setting['enclosures'])
|
||||||
|
# 关闭服务
|
||||||
|
yag.close()
|
||||||
|
logger.info("邮件发送成功!")
|
||||||
|
|
||||||
|
|
||||||
|
# if __name__ == '__main__':
|
||||||
|
# EmailServe.zip_report('../report/html', 'report.zip')
|
||||||
|
|
Loading…
Reference in New Issue