python3+beautifulReport+requests+excel+ddt+Jenkins之 beautifulReport部分

1、获取 BeautifulReport 包

BeautifulReport,git地址:

https://github.com/TesterlifeRaymond/BeautifulReport

1)如果自己电脑上安装了git,可以直接使用git命令克隆到本地指定目录下

git clone https://github.com/TesterlifeRaymond/BeautifulReport

 

2)如果没有安装git,直接在git上下载BeautifulReport.ZIP ,解压到到本地python的的/Lib/site-packages/目录下

 

 

 

3)修改BeautifulReport.py的2个地方

 

 

 

2、使用BeautifulReport,生成报告

 1 #coding=utf-8
 2 '''
 3 Created on 2020年8月13日
 4 
 5 @author: Administrator
 6 
 7 Descriptions: execute testcases
 8 '''
 9 import unittest
10 import os, datetime
11 from BeautifulReport import BeautifulReport
12 from tools.sendmail import SendMail
13 from config import conf
14 
15 root_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/')
16 test_dir = root_dir + '/testcases'#测试用例所在的目录
17 report_dir = root_dir + '/testreport'#存放测试报告的目录
18 
19 discover = unittest.defaultTestLoader.discover(test_dir, 'test_*.py', None)
20 now = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S')
21 filename = '测试报告' + str(now)
22 BeautifulReport(discover).report(description='测试', filename=filename, log_path=report_dir)
23 
24 #发送邮件
25 SendMail().sendEmail(filename)
26
27

 

3、BeautifulReport截图需要在测试类中添加一个save_img的方法

1 def save_img(self, img_name): 
2       self.driver.get_screenshot_as_file('{}/{}.png'.format(conf.img_dir, img_name))

然后调用时,只需要在测试方法上面加个装饰器就行了:

 1     """登录模块测试用例"""
 2     @BeautifulReport.add_test_img('登陆成功')
 3     def test_login1(self):
 4         """用户名正确,密码正确,登录成功"""
 5         self.login.loginFunc()
 6         self.save_img('登陆成功')
 7         time.sleep(3)
 8         currUrl = self.driver.current_url # 获取当前的url地址
 9         
10         try:
11             self.assertIn('index', currUrl, 'index not in current url!')#判断index在当前url中
12         except Exception:
13             self.login.saveScreenShot('cor_un_pw_fail.png')
14             raise
15         else:
16             self.login.saveScreenShot('cor_un_pw_pass.png')
17             log.logger.info('%s->run completed! ' % (sys._getframe().f_code.co_name))
18             self.login.quit()

 

4、测试报告样式

 

posted @ 2020-08-13 15:17  yanerfree  阅读(633)  评论(0编辑  收藏  举报