Common中邮件发送测试日志、截图、报告方法

import smtplib, os
from email.header import Header
from email.mime.text import MIMEText
from Log.Logs import TestRecord
from email.mime.multipart import MIMEMultipart


# 第三方 SMTP 服务
def send_mail(accepter):
    mail_host = "smtp.163.com"  # SMTP服务器
    mail_user = "xu354809049@163.com"  # 用户名
    mail_pass = "xxxxxxxx"  # 授权密码,非登录密码
    sender = 'xu354809049@163.com'  # 发件人邮箱
    receivers = ['' + accepter + '']  # 接收邮件,可设置为任意收件邮箱
    message = MIMEMultipart()
    message['From'] = Header("xu354809049@163.com")
    message['To'] = Header("各位负责人")
    subject = 'xxxxx平台UI自动化测试报告'
    message['Subject'] = Header(subject, 'utf-8')
    # 邮件正文内容
    message.attach(MIMEText('这是xxxxx的UI自动化测试报告,详情请见附件:', 'plain', 'utf-8'))
    # 构造附件1,传送log目录下的所有test.Log 文件
    for i in range(0, len(TestRecord().test_log())):
        att1 = MIMEText(
            open(os.path.dirname(os.path.dirname(__file__)) + '\\Log\\' + TestRecord().test_log()[i], 'rb').read(),
            'base64', 'utf-8')
        att1["Content-Type"] = 'application/octet-stream'
        # 这里的filename代表邮件中显示的名字
        att1["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_log()[i]
        message.attach(att1)

    # 构造附件2,传送test_report目录下的所有 result.html 文件
    for i in range(0, len(TestRecord().test_html())):
        att2 = MIMEText(
            open(os.path.dirname(os.path.dirname(__file__)) + '\\TestReport\\' + TestRecord().test_html()[i],
                 'rb').read(), 'base64', 'utf-8')
        att2["Content-Type"] = 'application/octet-stream'
        att2["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_html()[i]
        message.attach(att2)

    # 构造附件3,传送test_report目录下的所有 test.png 文件
    for i in range(0, len(TestRecord().test_image())):
        att3 = MIMEText(
            open(os.path.dirname(os.path.dirname(__file__)) + '\\TestReport\\Screen\\' + TestRecord().test_image()[i],
                 'rb').read(), 'base64', 'utf-8')
        att3["Content-Type"] = 'application/octet-stream'
        att3["Content-Disposition"] = 'attachment;filename=' + TestRecord().test_image()[i]
        message.attach(att3)
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("恭喜您,自动化测试报告已发送成功")

    except smtplib.SMTPException as e:
        print(e)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这个问题我可以回答。您可以使用Python编写一个App自动化测试框架,使用Appium进行自动化测试。以下是一些步骤: 1. 安装Python和Appium。 2. 安装必要的Python库,如selenium、pytest、allure-pytest等。 3. 编写测试脚本,使用selenium和Appium进行交互。 4. 集成日志模块,使用Python内置的logging库来生成日志。 5. 集成邮件发送模块,使用Python内置的smtplib库来发送邮件。 以下是一个简单的示例代码: ```python import logging import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from selenium.webdriver.common.by import By from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import pytest import allure class TestApp: @pytest.fixture(scope="session") def driver(self) -> WebDriver: # 初始化Appium driver # ... @allure.step("登录") def login(self, driver: WebDriver): # 登录测试代码 # ... @allure.step("测试用例1") def test_case1(self, driver: WebDriver): # 测试用例1代码 # ... @allure.step("测试用例2") def test_case2(self, driver: WebDriver): # 测试用例2代码 # ... @allure.step("测试用例3") def test_case3(self, driver: WebDriver): # 测试用例3代码 # ... @allure.step("生成日志") def generate_log(self): # 生成日志代码 # ... @allure.step("发送邮件") def send_email(self): # 邮件发送代码 # ... def test_main(self, driver: WebDriver): self.login(driver) self.test_case1(driver) self.test_case2(driver) self.test_case3(driver) self.generate_log() self.send_email() if __name__ == '__main__': test_app = TestApp() driver = test_app.driver test_app.test_main(driver) driver.quit() ``` 在这个示例,我们使用了pytest和allure来管理测试用例和测试报告。使用allure.step注释来记录每个测试步骤的名称。使用Python内置的logging库来生成日志,使用smtplib库来发送邮件。 当您运行测试脚本时,将生成一个包含所有测试结果的HTML报告,并将其保存在allure-report目录。如果测试失败,则将在控制台输出详细的错误消息,并生成包含错误信息的日志文件。如果测试成功,则将生成包含测试结果的日志文件。最后,将发送电子邮件,其包含生成的日志文件和测试报告。 希望这可以帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Automatic_tester

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值