詳解python3中用HTMLTestRunner.py報ImportError: No module named 'StringIO'如何解決
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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中使用asyncio實現(xiàn)異步IO實例分析
在本篇文章里小編給大家整理的是一篇關(guān)于python中使用asyncio實現(xiàn)異步IO實例分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02python scrapy框架中Request對象和Response對象的介紹
本文介紹了python基礎(chǔ)之scrapy框架中Request對象和Response對象的介紹,Request對象主要是用來請求數(shù)據(jù),爬取一頁的數(shù)據(jù)重新發(fā)送一個請求的時候調(diào)用,Response對象一般是由scrapy給你自動構(gòu)建的,因此開發(fā)者不需要關(guān)心如何創(chuàng)建Response對象,下面來一起來了解更多內(nèi)容吧2022-02-02python爬蟲模擬瀏覽器訪問-User-Agent過程解析
這篇文章主要介紹了python爬蟲模擬瀏覽器訪問-User-Agent過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12python對批量WAV音頻進行等長分割的方法實現(xiàn)
這篇文章主要介紹了python對批量WAV音頻進行等長分割的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python使用dlib進行人臉檢測和關(guān)鍵點的示例
這篇文章主要介紹了python使用dlib進行人臉檢測和關(guān)鍵點的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12tensorflow從ckpt和從.pb文件讀取變量的值方式
這篇文章主要介紹了tensorflow從ckpt和從.pb文件讀取變量的值方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05