1.下載BeautifulReport模塊html
下載地址:https://github.com/TesterlifeRaymond/BeautifulReportpython
2.解壓與存放路徑git
下載BeautifulReport的完整.ZIP文件,而後解壓,把整個文件包放到本地python的/Lib/site-packages/目錄下github
可能出現的錯誤,能夠參考地址:https://blog.csdn.net/chenmozhe22/article/details/82888060web
3.例子(web操做)json
目錄以下:測試
如下是Test_1.py文件代碼:ui
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()方法就好了spa
如下是run.py文件代碼:.net
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到報告裏面去,就生成了報告