unittest框架,漂亮的报告BeautifulReport配置与错误截图详细解说

1.下载BeautifulReport模块

下载地址:https://github.com/TesterlifeRaymond/BeautifulReport

2.解压与存放路径

下载BeautifulReport的完整.ZIP文件,然后解压,把整个文件包放到本地python的/Lib/site-packages/目录下

 可能出现的错误,可以参考地址:https://blog.csdn.net/chenmozhe22/article/details/82888060

3.例子(web操作)

目录如下:

 

以下是Test_1.py文件代码:

 1 import unittest,time,os
 2 from selenium import webdriver
 3 from BeautifulReport import BeautifulReport
 4 from selenium.webdriver.common.action_chains import ActionChains
 5 from selenium.webdriver.support.wait import WebDriverWait
 6 from selenium.webdriver.common.by import By
 7 from selenium.webdriver.support import expected_conditions as EC
 8 class Test_01(unittest.TestCase):
 9     def save_img(self, img_name):  #错误截图方法,这个必须先定义好
10         """
11             传入一个img_name, 并存储到默认的文件路径下
12         :param img_name:
13         :return:
14         """
15         self.driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath(r"G:\student_project\Hao\img"), img_name))    #os.path.abspath(r"G:\Test_Project\img")截图存放路径
16     def setUp(self):
17         print("开始测试")
18         self.driver = webdriver.Chrome()
19         self.driver.maximize_window()
20         self.driver.get("https://www.baidu.com/")
21     def tearDown(self):
22         print("结束测试")
23         self.driver.close()
24 
25     @BeautifulReport.add_test_img('test_case_1')     #装饰器,当你没有报错也要截图的话,那么你需要在用例里面调用save_img()方法
26     def test_case_1(self):   #用例没有错截图示例
27         WebDriverWait(self.driver,10).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='u1']/a[@name='tj_settingicon' and @class='pf']")))
28         ele=self.driver.find_element_by_xpath("//div[@id='u1']/a[@name='tj_settingicon' and @class='pf']")
29         ActionChains(self.driver).move_to_element(ele).perform()
30         self.driver.find_element_by_xpath('//a[@class="setpref"]').click()
31         WebDriverWait(self.driver, 10).until(
32             EC.visibility_of_element_located((By.XPATH, '//a[text()="保存设置"]')))
33         text_data=self.driver.find_element_by_xpath('//a[text()="保存设置"]').text
34         self.save_img("test_case_1")   #没有报错也要截图的话,直接在这里调用方法就行了
35         self.assertEqual("保存设置",text_data)
36     @BeautifulReport.add_test_img('test_case_2')     #装饰器,当你用例错误了,那么会自动调用save_img截图方法,存到指定目录下
37     def test_case_2(self):   #用例错误截图示例
38         time.sleep(1)
39         text_data = self.driver.find_element_by_xpath('//div[@id="u1"]/a').text
40         self.assertEqual("新闻1", text_data)

注意:如果想正确的用例也截图,那么你可以在用例上面添加装饰器,然后在用例里面调用save_img()方法就行了

以下是run.py文件代码:

 1 import unittest,time,os
 2 from BeautifulReport import BeautifulReport
 3 from Test_Case import Test_1
 4 current_path = os.getcwd()
 5 report_path = os.path.join(current_path, "Report")
 6 now = time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(time.time()))
 7 # 报告地址&名称
 8 report_title = 'Example报告' + now + ".html"  # 如果不能打开这个文件,可能是now的格式,不支持:和空格
 9 if __name__ == '__main__':
10     suite = unittest.TestSuite()  
11     loader=unittest.TestLoader()
12     suite.addTests(loader.loadTestsFromModule(Test_1))
13     #运行用例filename=报告名称,description=所有用例总的名称,report_path=报告路径,如果不填写默认当前执行文件目录,theme=报告的主题,有四种可以选择:theme_default,theme_cyan,theme_candy,theme_memories  默认是第一种
14     BeautifulReport(suite).report(filename="测试报告", description='Test_01模块',report_dir=report_path,theme="theme_cyan")

4.报告展示

说明:生成报告原理:他是读取了D:\python3.7.1\Lib\site-packages\BeautifulReport\template路径下面的theme_default.json基础数据,在读取template.html的数据,然后在写入你运行用例后的结果+body到报告里面去,就生成了报告

 

posted @ 2018-12-09 21:19  珠海-路飞  阅读(8115)  评论(3编辑  收藏  举报