python+selenium +unittest生成HTML測試報告


python+selenium+HTMLTestRunner+unittest生成HTML測試報告
首先要准備HTMLTestRunner文件,官網的HTMLTestRunner是python2語法寫的,看官手動把官網的HTMLTestRunner.py改成python3的語法:https://pan.baidu.com/s/1dEZQ0pz可下載修改之后的。
修改之后將HTMLTestRunner.py復制到python35的lib目錄下

from time import sleep
from selenium import webdriver
import HTMLTestRunner
import unittest
class login(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(5)
self.base_url = "https://www.baidu.com"
self.driver.maximize_window()
def test_login(self):
driver = self.driver
driver.get(self.base_url)
     #輸入selenium python
     driver.find_element_by_xpath("//input[@name='wd']").send_keys("selenium python")
     #點擊“百度”
driver.find_element_by_xpath("//input[@id='su']").click()
sleep(3)

def tearDown(self):
self.driver.quit()

#測試測試用例是否能正常執行 
# if __name__ == "__main__":
# unittest.main()

if __name__ == "__main__":
#定義一個測試容器
test = unittest.TestSuite()
#將測試用例,加入到測試容器中
test.addTest(login("test_login"))
#定義個報告存放的路徑,支持相對路徑
file_path = "C:\\Users\\000\\Pyresult\\sresult.html"
file_result= open(file_path, 'wb')
#定義測試報告
runner = HTMLTestRunner.HTMLTestRunner(stream = file_result, title = u"百度搜索測試報告", description = u"用例執行情況")
#運行測試用例
runner.run(test)
file_result.close()
最后的結果顯示如下:

 





免責聲明!

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



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