51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1753|回复: 3
打印 上一主题 下一主题

求助,怎么把这个测试报告以附件的形式发送邮件到邮箱?

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:16
  • 签到天数: 498 天

    连续签到: 5 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2019-10-24 10:22:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点
    我现在只做到了把测试报告以文件的形式发送到邮箱,请问怎么把这个测试报告以附件的形式发送邮件到邮箱?
    以下为源代码:
    1. # coding=utf-8
    2. from selenium import webdriver
    3. import time, unittest, os
    4. import HTMLTestRunner
    5. import smtplib
    6. from email.mime.text import MIMEText
    7. from email.header import Header


    8. class h5(unittest.TestCase):

    9.     def openweb1(b):
    10.         b = webdriver.Chrome()
    11.         time.sleep(2)
    12.         b.get('https://192.168.180.150/meeting/login')
    13.         b.maximize_window()
    14.         b.find_element_by_xpath('//*[@id="right"]/div[1]/input').send_keys('test001')
    15.         b.find_element_by_xpath('//*[@id="right"]/div[2]/input').send_keys('0003000')
    16.         b.find_element_by_xpath('//*[@id="right"]/div[3]/div').click()
    17.         time.sleep(2)
    18.         now_url = b.current_url

    19.         if now_url == "https://192.168.180.150/meeting/meetingToStart":
    20.             print("登录成功")
    21.         else:
    22.             raise error("登录失败")
    23.         b.quit()
    24.         
    25.     def openweb(b):
    26.         b = webdriver.Chrome()
    27.         time.sleep(2)
    28.         b.get('https://192.168.180.150/meeting/login')
    29.         b.maximize_window()
    30.         b.find_element_by_xpath('//*[@id="right"]/div[1]/input').send_keys('test001')
    31.         b.find_element_by_xpath('//*[@id="right"]/div[2]/input').send_keys('000000')
    32.         b.find_element_by_xpath('//*[@id="right"]/div[3]/div').click()
    33.         time.sleep(2)
    34.         now_url = b.current_url

    35.         if now_url == "https://192.168.180.150/meeting/meetingToStart":
    36.             print("登录成功")
    37.         else:
    38.             raise error("登录失败")
    39.         b.quit()

    40. def sendmail(file_new):
    41.     # 发信邮箱
    42.     mail_from = 'xxx@qq.com'
    43.     mail_to = ['xxx@qq.com']  # 收件箱
    44.     # 定义正文
    45.     f = open(file_new, 'rb')
    46.     mail_body = f.read()
    47.     f.close()
    48.     msg = MIMEText(mail_body, _subtype='html', _charset='utf-8')
    49.     # 定义标题
    50.     msg['Subject'] = u"自动化测试报告"
    51.     # 定义发送时间(不定义的可能有的邮件客户端会不显示发送时间)
    52.     msg['from'] = Header("xxx@qq.com", 'utf-8')
    53.     msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
    54.     smtp = smtplib.SMTP()
    55.     # 连接 SMTP 服务器,此处用的 qq 的 SMTP 服务器
    56.     smtp.connect('smtp.qq.com')
    57.     # 用户名密码
    58.     smtp.login('xxx@qq.com', 'fhqilugaqvilbcbi')
    59.     smtp.sendmail(mail_from, mail_to, msg.as_string())
    60.     smtp.quit()
    61.     print('email has send out')

    62. def send_report(testreport):
    63.         result_dir = testreport
    64.         lists = os.listdir(result_dir)
    65.         lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\\" + fn))
    66.         # print (u'最新测试生成的报告: '+lists[-1])
    67.         # 找到最新生成的文件
    68.         file_new = os.path.join(result_dir, lists[-1])
    69.         print(file_new)
    70.         # 调用发邮件模块
    71.         sendmail(file_new)

    72. testunit = unittest.TestSuite()

    73. testunit.addTest(h5("openweb"))
    74. testunit.addTest(h5("openweb1"))

    75. now = time.strftime("%Y-%m-%d-%H-%M-%S")

    76. # 定义报告存放路径,支持相对路径
    77. filePath = 'D://testcase//'
    78. filename = filePath + now + "result.html"
    79. fp = open(filename, 'wb')

    80. # 定义测试报告
    81. runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'测试报告', description=u'用例执行情况:')
    82. # 运行测试用例
    83. runner.run(testunit)
    84. fp.close()
    85. send_report(filePath)
    86. 下图为执行结果和邮件
    复制代码

    附件: 您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 11:08
  • 签到天数: 749 天

    连续签到: 27 天

    [LV.9]测试副司令

    2#
    发表于 2019-10-25 09:24:14 | 只看该作者
    可以写一段代码,抄送给上你想要发送的邮箱
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 07:18
  • 签到天数: 2778 天

    连续签到: 5 天

    [LV.Master]测试大本营

    3#
    发表于 2019-10-25 10:38:49 | 只看该作者
    可以的,
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 10:11
  • 签到天数: 1485 天

    连续签到: 1 天

    [LV.10]测试总司令

    4#
    发表于 2019-10-25 10:45:35 | 只看该作者
    可以加上个模块
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-9-28 07:21 , Processed in 0.077423 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表