HtmlTestRunner无法生成HTML报告问题
环境: Python3.6 + Selenium3.3.0 + HtmlTestRunner1.1.0 ON Windows10
IDE: PyCharm
HtmlTestRunner地址: https://github.com/oldani/HtmlTestRunner
今天在使用HTMLTestRunner时,测试用例可以顺利运行,但在目标目录下并未生成HTML文件,查找了网上的方法,也实验了几种方法,在我这都未生效。
后来捣鼓了很久,发现是IDE的问题,相同的代码在cmd中运行就可以生成HTML文件,在Pycharm中运行则不行。
问题: 在PyCharm上HTMLTestRunner无法生成HTML文件
代码示例:
1 # -*- coding: utf-8 -*- 2 from selenium import webdriver 3 from selenium.webdriver.common.by import By 4 from selenium.webdriver.common.keys import Keys 5 from selenium.webdriver.support.ui import Select 6 from selenium.common.exceptions import NoSuchElementException 7 from selenium.common.exceptions import NoAlertPresentException 8 import unittest, re 9 from time import sleep 10 import HtmlTestRunner 11 12 13 class Baidu(unittest.TestCase): 14 def setUp(self): 15 self.driver = webdriver.Chrome() 16 self.driver.implicitly_wait(30) 17 self.base_url = "https://www.baidu.com/" 18 self.verificationErrors = [] 19 self.accept_next_alert = True 20 21 def test_baidu_search(self): 22 driver = self.driver 23 driver.get(self.base_url) 24 driver.find_element_by_id("kw").click() 25 driver.find_element_by_id("kw").clear() 26 driver.find_element_by_id("kw").send_keys("selenium") 27 driver.find_element_by_id("su").click() 28 sleep(5) 29 driver.maximize_window() 30 sleep(3) 31 # 通过JavaScript设置浏览器窗口的滚动条位置 32 js = "window.scrollTo(100,450);" 33 driver.execute_script(js) 34 sleep(3) 35 driver.find_element_by_xpath(".//*[@id='8']/h3/a").click() 36 sleep(5) 37 driver.close() 38 39 def test_baidu_set(self): 40 driver = self.driver 41 driver.get(self.base_url) 42 driver.maximize_window() 43 sleep(3) 44 driver.find_element_by_xpath(".//*[@id='u1']/a[8]").click() 45 sleep(2) 46 driver.find_element_by_link_text(u"搜索设置").click() 47 sleep(3) 48 Select(driver.find_element_by_id("nr")).select_by_visible_text(u"每页显示20条") 49 driver.find_element_by_link_text(u"保存设置").click() 50 sleep(3) 51 driver.switch_to.alert.accept() 52 53 def tearDown(self): 54 self.driver.quit() 55 self.assertEqual([], self.verificationErrors) 56 57 58 if __name__ == "__main__": 59 unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner( 60 output='E:\\WorkItem\\TestItem\\testcase\\')) 61 62 """ 63 testunit = unittest.TestSuite() # 定义一个单元测试容器 64 testunit.addTest(Baidu("test_baidu_search")) 65 testunit.addTest(Baidu("test_baidu_set")) 66 67 runner = HtmlTestRunner.HTMLTestRunner( 68 output="E:/WorkItem/TestItem/testcase/", # 定义报告所写入的文件 69 report_title='My Report') 70 71 runner.run(testunit) 72 """
以上代码,依据网上看到的示例,尝试了三种路径编写方式,都未生效
用cmd运行脚本时,成功生成HTML文件
注意:在写HTML存放路径的时候,此处只能写目录dir