標籤:des style blog http io ar color 使用 sp
1、HTMLTestRunner 是 Python 標準庫的 unittest 模組的一個擴充。它產生便於使用的HTML 測試報告
1>下載HTMLTestRunner.py檔案,地址為:http://tungwaiyip.info/software/HTMLTestRunner.html
Windows平台:將下載的檔案放入...\Python27\Lib 目錄下Linux平台:下需要先確定 python 的安裝目錄,開啟終端,輸入 python 命令進入 python 互動模式,通過sys.path 可以查看本機 python 檔案目錄,以管理員身份將 HTMLTestRunner.py檔案考本到/usr/lib/python2.7/dist-packages/ 目錄下
2>匯入模組:import HTMLTestRunner。在代碼最尾部編寫代碼:
更好用的HTML報告
替換附件中的HTMLTestRunner.py。關於HTML報告中顯示出的方法在HTMLTestRunner中已經寫死,所以必須按寫死的路徑來設定檔案件。檔案夾順序如下:
對HTMLTestRunner中的總結如下:1、設定HTML報告的存放路徑為result2、設定儲存的路徑為result\image\。3、data裡面的圖片用來做表徵圖4、在代碼裡面把路徑給列印出來,具體效果如下:
# -*- coding: utf-8 -*-from selenium import webdriverimport unittestimport time,sysimport HTMLTestRunnerreload(sys)sys.setdefaultencoding("utf-8")class Baidu(unittest.TestCase): """百度首頁搜尋測試案例""" def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com" def test_baidu_search(self): driver = self.driver print u"========【case_0001】 百度搜尋=============" driver.get(self.base_url + "/") driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(u"林志玲") driver.find_element_by_id("su").click() now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) #必須要列印路徑HTMLTestRunner才能捕獲並且產生路徑,\image\**.png 是擷取路徑的條件,必須這樣的目錄
pic_path=‘..\\result\\image\\‘+now+‘.png‘ print pic_path driver.save_screenshot(pic_path) time.sleep(2) def tearDown(self): self.driver.quit() if __name__ == "__main__": now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) testunit = unittest.TestSuite() testunit.addTest(Baidu("test_baidu_search")) HtmlFile = "..\\result\\"+now+"HTMLtemplate.html" print HtmlFile fp = file(HtmlFile, "wb") runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"百度測試報告", description=u"用例測試情況") runner.run(testunit)
來自為知筆記(Wiz)
附件列表
04、產生 HTMLTestRunner 測試報告