#conding=utf-8
from HTMLTestRunner import HTMLTestRunner
import unittest
import time
import yagmail
dir = "./test_case"
suits = unittest.defaultTestLoader.discover(dir,pattern="test*.py") #discover方法实现了执行多个文件,将test开头的py文件添加到suits套件
if __name__ == '__main__':
file_dir = "./test_report/" + str(time.time()) + "result.html" #文件路径,取了时间戳为一部分,这样不会覆盖
file = open(file_dir,'wb') #写文件
runner = HTMLTestRunner(stream=file,title="csdn简单点击测试报告",description="练手而已") #stream指定文件
runner.run(suits) #执行套件
file.close()
subject = "csdn简单点击测试报告"
content = "正文"
yag = yagmail.SMTP(user="邮箱名称",password="授权密码",host="smtp.126.com") #连接邮箱
#password是POP3/SMTP/IMAP服务的授权密码,不然会报错:smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed')
yag.send("收件人邮箱名",subject=subject,contents=content,attachments=[file_dir,"C:\WiFi_Log.txt"]) #发送邮件,字段可以传list来实现多个参数
ps:
1.我的用例文件夹目录、报告目录、上面代码文件,在一个目录下
2.报告中的文字是之前测试用例中加的注释(""""""),在写用例的时候可以用这个作为用例名称