diff --git a/config/config.yaml b/config/config.yaml index d84e60b..9ebf4a6 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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: diff --git a/run.py b/run.py index 8f94a11..34bf42e 100644 --- a/run.py +++ b/run.py @@ -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() - - diff --git a/tools/send_email.py b/tools/send_email.py index 9b4b025..e42d361 100644 --- a/tools/send_email.py +++ b/tools/send_email.py @@ -9,23 +9,52 @@ """ import yagmail from tools import logger +import zipfile +import os -def send_email(setting): - """ - 入参一个字典 - :param user: 发件人邮箱 - :param password: 邮箱授权码 - :param host: 发件人使用的邮箱服务 例如:smtp.163.com - :param contents: 内容 - :param addressees: 收件人列表 - :param title: 邮件标题 - :param enclosures: 附件列表 - :return: - """ - yag = yagmail.SMTP(setting['user'], setting['password'], setting['host']) - # 发送邮件 - yag.send(setting['addressees'], setting['title'], setting['contents']) - # 关闭服务 - yag.close() - logger.info("邮件发送成功!") \ No newline at end of file +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: 发件人邮箱 + :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')