python 封装-生成报告-发送邮件

# -*- coding:utf-8 -*-
import unittest, os
from commen import HTMLTestRunner

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os


def send_email(smtpserver, port, sender, psw, receiver):
    # 写信模板
    msg = MIMEMultipart()
    msg['Subject'] = "这是项目自动化测试报告"
    msg['From'] = sender
    msg['to'] = receiver

    # 通过os获取文件路径
    current_path = os.getcwd()  # 获取当前脚本所在的文件夹路径
    report_path = os.path.join(current_path, "report") 
    annex_path = os.path.join(report_path, "report.html")  # 附件内容的路径
    annex = open(annex_path, "r", encoding="utf-8").read()

    main_body = 'xx领导好,自动化测试报告,请查收,自动发送,不用回复!'
    # main_body = open(body, "r", encoding="utf-8").read() 

    # 添加正文到容器
    body = MIMEText(main_body, "html", "utf-8")
    msg.attach(body)

    # 添加附件到容器
    att = MIMEText(annex, "base64", "utf-8")
    att["Content-Type"] = "application/octet-sream"
    att["Content-Disposition"] = 'attachment;filename="ssp_test_report.html"'
    msg.attach(att)

    # 连接发送邮件
    smtp = smtplib.SMTP_SSL(smtpserver, port)
    smtp.login(sender, psw)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()


def all_case():
    dir = os.getcwd()
    # print(dir)
    case_path = os.path.join(dir, 'case')
    # case_dir = "D:\\code\\0517\\test\\case"
    discover = unittest.defaultTestLoader.discover(
                                                    case_path,
                                                    pattern="test*.py"  # 执行test开头的py文件
                                                     )
    return discover


if __name__ == '__main__':
    cu_path = os.getcwd()
    report_path = os.path.join(cu_path, "report")
    result_path = os.path.join(report_path, "report.html")
    # result_path = "D:\\xxx\\xxx\\xxx\\report\\report.html"
    fb = open(result_path, "wb")

    runner = HTMLTestRunner.HTMLTestRunner(stream=fb,
                                           title="xx测试报告",
                                           description="自动化测试用例执行情况")

    runner.run(all_case())

    fb.close()

    # 发送邮件
    send_email("smtp.qq.com", 465, "108xx@qq.com", "xxx", "109@xxqq.com")


提前要设置好

 

文件相互关联进行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值