写在前面的话:
这个是我实际工作中写的项目,主要用来备注和后期查看~~大家可以参考学习,但是请不要用于其他不好的途径~~
准备工作:
先下载HTMLTestRunner.py
下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html
参考:http://www.cnblogs.com/testyao/p/5658200.html
里面提供写好的适合python3使用的:http://pan.baidu.com/s/1dEZQ0pz
把这个文件放在你安装python的lib目录下(我的在C:\Program Files\Python36\Lib)
一:少量用例,仅生成测试报告
#############run1.py#############
1 import unittest 2 import HTMLTestRunner 3 import os 4 5 #从文件test_ljj.py加载AddCompany类 6 from test_case.company.test_getLog import GetLog 7 8 #使用测试套件,添加Case01类里面的test_ case01方法 9 suite = unittest.TestSuite() 10 suite.addTest(GetLog('test_getLogN')) 11 12 #{这一段是输出到特定目录下 13 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__))) 14 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件所在目录 15 report_path = os.path.join(cur_path, 'report') #拼接成一个新目录 16 report_abspath = os.path.join(report_path, "result.html") 17 18 #以字节的方式写入目录下的report.html文件里 19 st = open(report_abspath, 'wb') 20 HTMLTestRunner.HTMLTestRunner(stream = st, title= '公司端接口自动化测试报告').run(suite) 21 #} 22 23 # 这种方法是直接在控制台输出 24 # runner = unittest.TextTestRunner() 25 # runner.run(suite)
参考网址:http://www.cnblogs.com/testyao/p/5658200.html
或者http://www.cnblogs.com/miniren/p/5301081.html#commentform
二、执行多条用例,仅生成测试报告
#############run2.py#############
1 #coding=utf-8 2 import unittest 3 import os 4 import HTMLTestRunner 5 6 #仅生成测试报告不发送邮件 7 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__))) 8 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径 9 case_path = os.path.join(cur_path, 'test_case') #测试case目录 10 report_path = os.path.join(cur_path, 'report') #测试报告存放目录 11 12 13 def all_case(): 14 '''第一步:加载所有的测试用例''' 15 16 # 批量加载iscover方法里面有三个参数:-case_dir:这个是待执行用例的目录-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。 17 # discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里,这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。 18 19 discover = unittest.defaultTestLoader.discover(case_path, 20 pattern="test_ljj*.py", 21 top_level_dir=None) 22 # print(discover) 23 return discover 24 25 def run_case(all_case): 26 '''第二步:执行所有的用例, 并把结果写入HTML测试报告''' 27 28 # 测试报告文件路径 29 report_abspath = os.path.join(report_path, "result.html") 30 fp = open(report_abspath, "wb") 31 # 批量执行测试用例三个参数: --stream:测试报告写入文件的存储区域 --title:测试报告的主题 --description:测试报告的描述 32 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, 33 title=u'日本公司端自动化测试报告,测试结果如下:', 34 description=u'用例执行情况:') 35 36 # 调用add_case函数返回值 37 runner.run(all_case) 38 fp.close() 39 40 if __name__ == "__main__": 41 all_case = all_case() # 1加载用例 42 run_case(all_case) # 2执行用例
#############run3.py#############
1 # -* - coding: UTF-8 -* - 2 3 [email] 4 smtp_server = smtp.163.com 5 port = 587 6 sender = ljjiaoyou@163.com 7 psw = Aa!23456 8 receiver = 1628891265@qq.com 9 10 [website] 11 host = http://10.95.178.216:8001 12 url = /gulfstream/mis/i18n-apac-mis/company 13 14 [payload] 15 page =1 16 size = 50 17 locale = zh-CN 18 utc_offset = 480 19 canonical_country_code = JP
1 # -* - coding: UTF-8 -* - 2 import os 3 from backports.configparser import ConfigParser 4 5 cur_path = os.path.dirname(os.path.realpath(__file__)) #获取当前文件所在路径 6 configPath = os.path.join(cur_path, "cfg.ini") #拼接出文件路径 7 conf = ConfigParser() 8 conf.read(configPath) #读取配置文件cfg.ini 9 10 # 获取指定的section, 指定的option的值 11 smtp_server = conf.get("email", "smtp_server") 12 sender = conf.get("email", "sender") 13 psw = conf.get("email", "psw") 14 receiver = conf.get("email", "receiver") 15 port = conf.get("email", "port") 16 17 host = conf.get("website", "host") 18 url = conf.get("website", "url") 19 20 # 读取该section下的所有值,并以键值对形式输出。格式为list 21 payload1 = conf.items("payload") 22 payload = dict(payload1)
1 #coding=utf-8 2 import unittest 3 import os 4 import HTMLTestRunner 5 import smtplib 6 from email.mime.text import MIMEText 7 from email.mime.multipart import MIMEMultipart 8 9 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__))) 10 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径 11 case_path = os.path.join(cur_path, 'test_case') #测试case目录 12 report_path = os.path.join(cur_path, 'report') #测试报告存放目录 13 14 def send_mail(sender, psw, receiver, smtpserver, port): 15 '''第三步:发送最新的测试报告内容''' 16 file_path = report_path + r"\result.html" 17 with open(file_path, "rb") as f: 18 mail_body = f.read() 19 20 # 定义邮件内容 21 msg = MIMEMultipart() 22 msg['Subject'] = u"就是想测试下看看能不能收到邮件" #主题 23 msg["from"] = sender #发件人 24 msg["to"] = receiver #收件人 25 26 #正文 27 body = MIMEText(mail_body, _subtype='html', _charset='utf-8') 28 msg.attach(body) 29 30 # 添加附件 31 att = MIMEText(mail_body, "base64", "utf-8") 32 att["Content-Type"] = "application/octet-stream" 33 att["Content-Disposition"] = 'attachment; filename= "report.html"' 34 msg.attach(att) 35 36 try: 37 smtp = smtplib.SMTP_SSL(smtpserver, port) #QQ邮箱 38 except: 39 smtp = smtplib.SMTP() #163邮箱 40 smtp.connect(smtpserver, port) #链接服务器 41 # 用户名密码 42 smtp.login(sender, psw) #登录 43 smtp.sendmail(sender, receiver, msg.as_string())#发送 44 smtp.quit() #关闭 45 print('test report email has send out !') 46 47 48 if __name__ == "__main__": 49 # 读取邮箱配置 50 from config import readConfig 51 sender = readConfig.sender 52 psw = readConfig.psw 53 smtp_server = readConfig.smtp_server 54 port = readConfig.port 55 receiver = readConfig.receiver 56 send_mail(sender, psw, receiver, smtp_server, port) # 发送报告
四、想把执行case和发邮件放在一起的话,把run2.py和run3.py合并即可
参考:https://www.cnblogs.com/yoyoketang/p/7259993.html
#############run4.py#############
1 #coding=utf-8 2 import unittest 3 import os 4 import HTMLTestRunner 5 import smtplib 6 from email.mime.text import MIMEText 7 from email.mime.multipart import MIMEMultipart 8 9 # 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__))) 10 cur_path = os.path.dirname(os.path.realpath(__file__)) #当前文件存在的路径 11 case_path = os.path.join(cur_path, 'test_case') #测试case目录 12 report_path = os.path.join(cur_path, 'report') #测试报告存放目录 13 14 def all_case(): 15 '''第一步:加载所有的测试用例''' 16 17 # 批量加载iscover方法里面有三个参数:-case_dir:这个是待执行用例的目录-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。 18 # discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里,这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。 19 20 discover = unittest.defaultTestLoader.discover(case_path, 21 pattern="test_lj*.py", 22 top_level_dir=None) 23 # print(discover) 24 return discover 25 26 # def run_case(): 27 def run_case(all_case): 28 '''第二步:执行所有的用例, 并把结果写入HTML测试报告''' 29 # 测试报告文件路径 30 report_abspath = os.path.join(report_path, "result.html") 31 fp = open(report_abspath, "wb") 32 # 批量执行测试用例三个参数: --stream:测试报告写入文件的存储区域 --title:测试报告的主题 --description:测试报告的描述 33 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, 34 title=u'日本公司端自动化测试报告,测试结果如下:', 35 description=u'用例执行情况:') 36 37 # 调用add_case函数返回值 38 runner.run(all_case) 39 fp.close() 40 41 def send_mail(sender, psw, receiver, smtpserver, port): 42 '''第三步:发送最新的测试报告内容''' 43 file_path = report_path + r"\result.html" 44 with open(file_path, "rb") as f: 45 mail_body = f.read() 46 47 # 定义邮件内容 48 msg = MIMEMultipart() 49 msg['Subject'] = u"就是想测试下看看能不能收到邮件" #主题 50 msg["from"] = sender #发件人 51 msg["to"] = receiver #收件人 52 53 #正文 54 body = MIMEText(mail_body, _subtype='html', _charset='utf-8') 55 msg.attach(body) 56 57 # 添加附件 58 att = MIMEText(mail_body, "base64", "utf-8") 59 att["Content-Type"] = "application/octet-stream" 60 att["Content-Disposition"] = 'attachment; filename= "report.html"' 61 msg.attach(att) 62 63 try: 64 smtp = smtplib.SMTP_SSL(smtpserver, port) #QQ邮箱 65 except: 66 smtp = smtplib.SMTP() #163邮箱 67 smtp.connect(smtpserver, port) #链接服务器 68 # 用户名密码 69 smtp.login(sender, psw) #登录 70 smtp.sendmail(sender, receiver, msg.as_string())#发送 71 smtp.quit() #关闭 72 print('test report email has send out !') 73 74 if __name__ == "__main__": 75 all_case = all_case() # 1加载用例 76 # 生成测试报告的路径 77 run_case(all_case) # 2执行用例 78 # 邮箱配置 79 from config import readConfig 80 sender = readConfig.sender 81 psw = readConfig.psw 82 smtp_server = readConfig.smtp_server 83 port = readConfig.port 84 receiver = readConfig.receiver 85 send_mail(sender, psw, receiver, smtp_server, port) # 发送报告
六、项目文件分布图