使用Python ,HTMLTestRunner 生成测试报告时,遇到很奇怪的问题,明明运行的结果,没有任何报错,就是不生成测试报告,纠结好久。google+baidu搜索结果也不满意,最后终于解决,先总结下。
代码示例
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import Selectfrom selenium.common.exceptions import NoSuchElementExceptionimport unittest, time, re,osimport HTMLTestRunnerclass Baidu(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome(r‘C:\Users\user\AppData\Local\Google\Chrome\Application\chromedriver‘) self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True def test_baidu_search(self): driver = self.driver driver.get(self.base_url + "/") driver.find_element_by_id("kw").send_keys("selenium webdriver") driver.find_element_by_id("su").click() time.sleep(2) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": testunit = unittest.TestSuite() # 将测试用例加入到测试容器中 testunit.addTest(Baidu("test_baidu_search")) # 定义个报告存放路径,支持相对路径 filename = ‘E:\\sresult.html‘ fp = open(filename, ‘wb‘) # 定义测试报告 runner=HTMLTestRunner.HTMLTestRunner( stream=fp, title=‘baidu‘, description=‘baidu:‘ ) # 运行测试用例 runner.run(testunit)
原文地址:https://www.cnblogs.com/linalove/p/9896867.html
时间: 2024-11-07 18:31:45