import unittest
import webbrowser
import HTMLTestRunner #生成报告用
from email.mime.text import MIMEText #构造邮件用
from email.mime.multipart import MIMEMultipart #构造邮件用
import smtplib #发送邮件用
from email.header import Header
from lsxm.mytest import mytestcaseA #测试用例
from lsxm.mytest.mytestcaseA import mytestcase01 #测试用例
import mytestcaseB
from mytestcaseB import mytestcase02
def creatsuit_one():
# 执行器会按照用例的添加顺序执行各用例,并聚合结果
uper1 = mytestcase01(methodName="test_uper01")
add1 = mytestcase01(methodName="test_add01")
suit = unittest.TestSuite()
suit.addTest(uper1)
suit.addTest(add1)
# a = suit.countTestCases()
return suit
def creatsuit_two():
map_obj = map(mytestcase02, ["test_uper02", "test_add02"])
suit = unittest.TestSuite()
suit.addTests(map_obj) #注意用复数的addtests
return suit
# 返回testCaseClass中包含的所有测试用例的suite
def creatsuit_three():
# 用例函数必须以test开头,否则无法识别,就不会执行
# loadTestsFromTestCase实际内部会走map函数,自动生成各个用例对象
suit01 = unittest.TestLoader().loadTestsFromTestCase(testCaseClass=mytestcase01)
suit02 = unittest.TestLoader().loadTestsFromTestCase(testCaseClass=mytestcase02)
suit = unittest.TestSuite()
suit.addTests([suit01,suit02])
return suit
# 返回包含在给定模块中的所有测试用例的suite
# noinspection PyUnusedLocal
def creatsuit_four():
suit01 = unittest.TestLoader().loadTestsFromModule(module = mytestcaseA)
suit02 = unittest.TestLoader().loadTestsFromModule(module = mytestcaseB)
suit = unittest.TestSuite()
suit.addTests([suit01,suit02])
return suit
# 返回指定字符串的所有测试用例的suite
def creatsuit_five():
suit01 = unittest.TestLoader().loadTestsFromName(name = "mytestcase01.test_add01",module = mytestcaseA)
suit02 = unittest.TestLoader().loadTestsFromName(name = "mytestcase02.test_uper02",module = mytestcaseB)
suit = unittest.TestSuite()
suit.addTests([suit01, suit02])
return suit
# 返回指定字符串的所有测试用例的suite
def creatsuit_six():
suit01 = unittest.TestLoader().loadTestsFromNames(names = ["mytestcase01.test_add01","mytestcase01.test_uper01"],module = mytestcaseA)
suit02 = unittest.TestLoader().loadTestsFromNames(names = ["mytestcase02.test_add02","mytestcase02.test_uper02"],module = mytestcaseB)
suit = unittest.TestSuite()
suit.addTests([suit01, suit02])
return suit
#从指定的目录开始递归查找所有测试模块(含子目录)(只识别Python的包,也就是目录内有__init__.py文件的才算是Python的包)
def creatsuit_seven():
suit = unittest.TestLoader().discover(r"H:/pycharm/test",pattern="*testcase*.py")
return suit
# 放松邮件
def sendmail():
# 第三方 SMTP 服务
mail_host = "smtp.qq.com" # 设置服务器
mail_user = "394659503@qq.com" # 用户名
mail_pass = "yhowidvprkoxbhga" #"wllxzewvczpqbhga" #yhowidvprkoxbhga qq邮箱pop/smtp" #WLAIXNIQNAIYDCXG 163邮箱" # 口令
# 设置收件人和发件人
sender = '394659503@qq.com'
receivers = ['a1822087@163.com','gaoxia280119@163.com','394659503@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 创建一个带附件的实例对象
message = MIMEMultipart() #MIMEMultipart的父类message的__str__会把对象面位list
# 邮件主题、收件人、发件人
subject = '请查阅--测试报告' # 邮件主题
message['Subject'] = Header(subject, 'utf-8')
message['From'] = Header("{}".format(sender), 'utf-8') # 发件人
message['To'] = Header("{}".format(';'.join(receivers)), 'utf-8') # 收件人
# send_content = get_case_result() # 获取测试报告
with open('report1.html','r',encoding='utf-8') as f:
send_content = f.read()
# send_content = "1111"
html = MIMEText(_text=send_content, _subtype='html', _charset='utf-8') # 第一个参数为邮件内容
# 构造附件
att = MIMEText(_text=send_content, _subtype='base64', _charset='utf-8')
att["Content-Type"] = 'application/octet-stream'
file_name = 'report1.html'
att["Content-Disposition"] = 'attachment; filename="{}"'.format(file_name) # # filename 为邮件附件中显示什么名字
message.attach(html)
message.attach(att)
try:
smtp_obj = smtplib.SMTP()
smtp_obj.connect(mail_host, 25) # 25 为 SMTP 端口号
smtp_obj.login(mail_user, mail_pass)
smtp_obj.sendmail(sender, receivers, message.as_string())
smtp_obj.quit()
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
if __name__ == '__main__':
# 执行suit添加的用例
#******************************************************************
# suit = creatsuit_one()
# suit = creatsuit_two()
# suit = creatsuit_three()
# suit = creatsuit_four()
# suit = creatsuit_five()
# suit = creatsuit_six()
suit = creatsuit_seven()
# runner = unittest.TextTestRunner(verbosity=1)
# runner.run(suit)
#******************************************************************
# main()执行所有"test"开头的用例
# unittest.main()
# ******************************************************************
#生成测试报告
filename ="report1.html"
file1 = open("filename","wb")
HTMLTestRunner.HTMLTestRunner(stream=file1, verbosity=1, title="UI自动化测试报告", description="高霞的测试报告").run(suit)
file1.close()
# webbrowser.open("filename")
#发送邮件
sendmail()
#在Terminal中在project目录下执行python TestSuit.py -v
# -v:详细显示测试结果