1. 程式人生 > >python使用HTMLTestRunner.py生成測試報告

python使用HTMLTestRunner.py生成測試報告

http 加載驅動 send url close .com 關閉 stun 註意

這裏我使用的是python selenium webdriver環境,瀏覽器驅動安裝見selenium

1、下載HTMLTestRunner.py:http://tungwaiyip.info/software/HTMLTestRunner.html

  python3環境需要對該文件的六個地方進行修改,修改後放置python\Lib目錄下

from selenium import webdriver
import HTMLTestRunner,unittest,time,os

class BaiduTest(unittest.TestCase):
    def setUp(self): #
測試準備工作 self.driver = webdriver.Chrome() #加載驅動 self.driver.implicitly_wait(30) #隱式等待 self.base_url = "http://www.baidu.com" #url def test_baidu(self): #測試用例 driver = self.driver print("========【case_0001】 百度搜索=============") driver.get(self.base_url
+ "/") #get發送url driver.find_element_by_id("kw").clear() #清空 driver.find_element_by_id("kw").send_keys("shuzf") #搜索shuzf driver.find_element_by_id("su").click() #點擊 time.sleep(5)#休息5s self.assertEqual("shuzf_百度搜索", driver.title) # 斷言判斷是否相等 now = time.strftime("
%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) if not os.path.exists(result/image/): os.makedirs(result/image/) #判斷當前路徑是否存在,沒有則創建文件夾 pic_path = result/image/ + now + .png driver.save_screenshot(pic_path) #生成圖片 def tearDown(self): #釋放資源 self.driver.quit() #退出 if __name__ == "__main__": testunit = unittest.TestSuite() #構造測試套件 testunit.addTest(BaiduTest("test_baidu")) #添加測試用例 #HtmlFile = "c:/"+now+"run.html" now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) if not os.path.exists(result/): os.makedirs(result/) #判斷當前路徑是否存在,沒有則創建文件夾 HtmlFile = "result/" + now + ".html" fp = open(HtmlFile, "wb") #生成報告 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title="百度測試報告", description="用例測試情況") runner.run(testunit) #執行套件 fp.close() #關閉 #註意點1、瀏覽器驅動的安裝 2,文件夾的生成

2,使用命令行執行文件,而不是pycharm

  >python demo.py

python使用HTMLTestRunner.py生成測試報告