詳解python3中用HTMLTestRunner.py報(bào)ImportError: No module named 'StringIO'如何解決
python3中用HTMLTestRunner.py報(bào)ImportError: No module named 'StringIO'的解決方法:
1.原因是官網(wǎng)的是python2語法寫的,看官手動(dòng)把官網(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,開始時(shí),引入HTMLTestRunner模塊報(bào)錯(cuò)。
在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__":
#定義一個(gè)測(cè)試容器
test = unittest.TestSuite()
#將測(cè)試用例,加入到測(cè)試容器中
test.addTest(Baidu("test_baidu"))
#定義個(gè)報(bào)告存放的路徑,支持相對(duì)路徑
file_path = "F:\\RobotTest\\result.html"
file_result= open(file_path, 'wb')
#定義測(cè)試報(bào)告
runner = HTMLTestRunner.HTMLTestRunner(stream = file_result, title = u"百度搜索測(cè)試報(bào)告", description = u"用例執(zhí)行情況")
#運(yùn)行測(cè)試用例
runner.run(test)
file_result.close()
運(yùn)行測(cè)試腳本后,發(fā)現(xiàn)報(bào)錯(cuò):
File "C:\Python34\lib\HTMLTestRunner.py", line 642, in sortResult
if not rmap.has_key(cls):
所以前往642行修改代碼:
運(yùn)行后繼續(xù)報(bào)錯(cuò):
AttributeError: 'str' object has no attribute 'decode'
前往766, 772行繼續(xù)修改(注意:766行是uo而772行是ue,當(dāng)時(shí)眼瞎,沒有注意到這些,以為是一樣的,導(dǎo)致報(bào)了一些莫名其妙的錯(cuò)誤,折騰的半天):
修改后運(yùn)行,發(fā)現(xiàn)又報(bào)錯(cuò):
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)整個(gè)程序中,唯一一個(gè)print:
print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime
這個(gè)是2.x的寫法,咱們修改成3.x的print,修改如下:
print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3編碼問題 Unicode utf-8 bytes互轉(zhuǎn)方法
今天小編就為大家分享一篇Python3編碼問題 Unicode utf-8 bytes互轉(zhuǎn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
python正則表達(dá)式常見的知識(shí)點(diǎn)匯總
正則表達(dá)式提供了一些可用的匹配模式,比如忽略大小寫、多行匹配等,下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式常見的知識(shí)點(diǎn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
Python HTTP下載文件并顯示下載進(jìn)度條功能的實(shí)現(xiàn)
這篇文章主要介紹了Python HTTP下載文件并顯示下載進(jìn)度條功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容操作代碼
這篇文章主要介紹了Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容,基本導(dǎo)入是在Python中使用pandas導(dǎo)入.xlsx文件的方法是read_excel(),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12







