python自动化通过邮件发送测试结果

 
#################
#单人发送文本文件
#################
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def sendReport():
    #发送邮箱
    sender = '[email protected]'
    #接收邮箱
    receivers = ['[email protected]','[email protected]']   #多人发送
    # receiver = '[email protected]'  #单人发送
    #发送邮件主题
    subject = '文本邮箱'
    #发送邮箱服务器
    smtpserver = 'smtp.qq.com'
    #发送邮箱用户/密码
    username = '[email protected]'
    password = 'XXXXXXXX'#授权码
    #中文需参数‘utf-8’,单字节字符不需要
    #编写HTML类型的邮件正文
    msg = MIMEText('<html><h1>测试邮件!</h1></html>','html','utf-8')
    #主题固定属性
    msg['Subject'] = Header(subject, 'utf-8')
    #发送者
    msg['from'] = sender
    # 接受者
    # msg['to'] = receiver
    #连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    #smtp.sendmail(sender, receivers, msg.as_string())
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

sendReport()
以qq邮箱为例,获取授权码的步骤:
python自动化通过邮件发送测试结果
python自动化通过邮件发送测试结果

python自动化通过邮件发送测试结果

python自动化通过邮件发送测试结果
python自动化通过邮件发送测试结果



#################
#多人发送文本文件
#################
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def sendReport():
    #发送邮箱
    sender = '[email protected]'
    #接收邮箱
    # receivers = ['[email protected]','[email protected]']   #多人发送
    receiver = '[email protected]'  #单人发送
    #发送邮件主题
    subject = '看片神器,以附件的形式'
    #发送邮箱服务器
    smtpserver = 'smtp.qq.com'
    #发送邮箱用户/密码
    username = '[email protected]'
    password = 'XXXXXX'#授权码
    #中文需参数‘utf-8’,单字节字符不需要
    #编写HTML类型的邮件正文
    msg = MIMEText('<html><h1>短信风闸!</h1></html>','html','utf-8')
    #主题固定属性
    msg['Subject'] = Header(subject, 'utf-8')
    #发送者
    msg['from'] = sender
    # 接受者
    msg['to'] = receiver
    #连接发送邮件
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)
    # smtp.sendmail(sender, receivers, msg.as_string())
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

sendReport()
结果:
python自动化通过邮件发送测试结果

html报告发送邮件,以百度关键字搜索为例
Baidu.py
from selenium import webdriver
from time import sleep
import unittest

# driver = webdriver.Chrome()
# driver.get('http://www.baidu.com')
# driver.find_element_by_xpath('//*[@id="kw"]').send_keys('selenium')
# sleep(2)
# driver.find_element_by_xpath('//*[@id="su"]').click()
# sleep(2)
# driver.quit()

class Baidu(unittest.TestCase):
    """百度搜索测试"""
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.base_url = 'http://www.baidu.com'

    def test_baidu_search(self):
        driver = self.driver
        driver.get(self.base_url)
        driver.find_element_by_xpath('//*[@id="kw"]').send_keys('selenium')
        sleep(2)
        driver.find_element_by_xpath('//*[@id="su"]').click()
        sleep(2)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
        unittest.main()
send_email.py
from HTMLTestRunner import HTMLTestRunner
from email.mime.text import MIMEText
from email.header import Header

import smtplib
import unittest
import time
import os

# 发送邮件
def sendReport(file_new):
    with open(file_new,'rb') as f:
        mail_body = f.read()
    # 构造MIMEText对象,作为邮件以内容的形式附加
    msg = MIMEText(mail_body,'html','utf-8')
    msg['Subject'] = Header('Baidu自动化测试脚本报告','utf-8')
    msg['from'] = "[email protected]" #发送地址
    msg['to'] = "[email protected]"    #收件地址

    stmp = smtplib.SMTP()
    stmp.connect('smtp.qq.com')
    stmp.login('[email protected]','XXXXXX') #邮箱账号和密码(授权密码)
    stmp.sendmail(msg['from'],msg['to'].split(':'),msg.as_string())

    stmp.quit()
    print('The Html Report is send out')

def newReport(testReport):
    lists = os.listdir(testReport)
    lists2 = sorted(lists)  #排序
    file_new = os.path.join(testReport,lists2[-1])#获取最新的html测试报告
    print(file_new)
    return  file_new

if __name__ == '__main__':
    test_dir = 'C:\\Users\\apple\\PycharmProjects\\pycase\\email'#测试用例路径
    test_report = 'C:\\Users\\apple\\PycharmProjects\\pycase\\email\\Result'#测试报告路径

    discover = unittest.defaultTestLoader.discover(test_dir,pattern='Baidu.py')
    now = time.strftime("%Y-%m-%d_%H_%M_%S", time.localtime())#获取当前时间
    filename = test_report + '\\' + now + 'result.html'
    fp = open(filename,'wb')
    runner = HTMLTestRunner(stream=fp,title='测试报告',description='测试脚本执行情况')
    runner.run(discover)
    fp.close()

    new_report = newReport(test_report)
    print(new_report)
    sendReport(new_report)  #发送测试报告邮件

结果:
python自动化通过邮件发送测试结果
'''
发送邮件总结
1.python smtplib格式
import smptlib
smtpobject = smtplibSMTP(host,port,local_hostname)
host:运行SMTP服务器的主机
port:端口
Local_hostname:在smtp本地运行,则可以制定localhost
2.SMTP对象有一个sendmail的实例方法,用于执行邮件发送
Sender:具有发件人地址的字符串
Receivers:字符串列表,每个收件人一样
Message:作为格式如在各种RFC中指定的格式
3.给单人发送邮件
'''