python3中用HTMLTestRunner.py報ImportError: No module named 'StringIO'的解決方法:
1.原因是官網(wǎng)的是python2語法寫的,看官手動把官網(wǎng)的HTMLTestRunner.py改成python3的語法:
參考:http://bbs.chinaunix.net/thread-4154743-1-1.html
下載地址:http://tungwaiyip.info/software/HTMLTestRunner.html
修改后下載地址:HTMLTestRunner_jb51.rar (懶人直接下載吧)
2.修改匯總:
第94行,將import StringIO修改成import io
第539行,將self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()
第642行,將if not rmap.has_key(cls):修改成if not cls in rmap:
第766行,將uo = o.decode('latin-1')修改成uo = e
第775行,將ue = e.decode('latin-1')修改成ue = e
第631行,將print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
在Python3.4下使用HTMLTestRunner,開始時,引入HTMLTestRunner模塊報錯。
在HTMLTestRunner的94行中,是使用的StringIO,但是Python3中,已經(jīng)沒有StringIO了。取而代之的是io.StringIO。所以將此行修改成import io
在HTMLTestRunner的539行中,self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()
修改以后,成功引入模塊了
執(zhí)行腳本代碼:
# -*- coding: utf-8 -*- #引入webdriver和unittest所需要的包 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import NoAlertPresentException import unittest, time, re #引入HTMLTestRunner包 import HTMLTestRunner class Baidu(unittest.TestCase): #初始化設(shè)置 def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True #百度搜索用例 def test_baidu(self): driver = self.driver driver.get(self.base_url) driver.find_element_by_id("kw").click() driver.find_element_by_id("kw").clear() 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__": #定義一個測試容器 test = unittest.TestSuite() #將測試用例,加入到測試容器中 test.addTest(Baidu("test_baidu")) #定義個報告存放的路徑,支持相對路徑 file_path = "F:\\RobotTest\\result.html" file_result= open(file_path, 'wb') #定義測試報告 runner = HTMLTestRunner.HTMLTestRunner(stream = file_result, title = u"百度搜索測試報告", description = u"用例執(zhí)行情況") #運行測試用例 runner.run(test) file_result.close()
運行測試腳本后,發(fā)現(xiàn)報錯:
File "C:\Python34\lib\HTMLTestRunner.py", line 642, in sortResult
if not rmap.has_key(cls):
所以前往642行修改代碼:
運行后繼續(xù)報錯:
AttributeError: 'str' object has no attribute 'decode'
前往766, 772行繼續(xù)修改(注意:766行是uo而772行是ue,當(dāng)時眼瞎,沒有注意到這些,以為是一樣的,導(dǎo)致報了一些莫名其妙的錯誤,折騰的半天):
修改后運行,發(fā)現(xiàn)又報錯:
File "C:\Python34\lib\HTMLTestRunner.py", line 631, in run
print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'
前往631查看,發(fā)現(xiàn)整個程序中,唯一一個print:
print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime
這個是2.x的寫法,咱們修改成3.x的print,修改如下:
print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
