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到報告里面去,就生成了報告

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM