The solution for reporting ImportError: No module named 'StringIO' with HTMLTestRunner.py in python3:

The full text is reproduced to: http://www.cnblogs.com/testyao/p/5658200.html

 

The solution for reporting ImportError: No module named 'StringIO' with HTMLTestRunner.py in python3:

 

1. The reason is that the official website is written in python2 syntax, and the official manually changed the HTMLTestRunner.py of the official website to python3 syntax:

Reference: http://bbs.chinaunix.net/thread-4154743-1-1.html

Download address: http://tungwaiyip.info/software/HTMLTestRunner.html

The revised download address: http://pan.baidu.com/s/1dEZQ0pz    (lazy people download it directly)

 

2. Modification summary:

Line 94, modify import StringIO to import io

Line 539, modify self.outputBuffer = StringIO.StringIO() to self.outputBuffer = io.StringIO()

Line 642, change if not rmap.has_key(cls): to if not cls in rmap:

Line 766, modify uo = o.decode('latin-1') to uo = e

Line 775, change ue = e.decode('latin-1') to ue = e

第631行,将print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

Using HTMLTestRunner under Python 3.4, at the beginning, the HTMLTestRunner module was introduced to report an error.

1

In line 94 of HTMLTestRunner, StringIO is used, but in Python3, StringIO is no longer available. In its place is io.StringIO. So modify this line to import io

2

In line 539 of HTMLTestRunner, self.outputBuffer = StringIO.StringIO() is modified to self.outputBuffer = io.StringIO()

3

After modification, the module was successfully imported

4

 

Execute script code:

copy code
copy code
# -*- coding: utf-8 -*-
#Introduce the packages required by webdriver and 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

#Introduce HTMLTestRunner package
import HTMLTestRunner   

class Baidu(unittest.TestCase):
    #Initialize settings
    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
    
    #Baidu search use case
    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__":
    #define a test container
    test = unittest.TestSuite()

    #Add the test case to the test container
    test.addTest(Baidu("test_baidu"))

    #Define the path where the report is stored, support relative path
    file_path = "F:\\RobotTest\\result.html"
    file_result= open(file_path, 'wb')

    #define test report
    runner = HTMLTestRunner.HTMLTestRunner(stream = file_result, title = u"Baidu search test report", description = u"use case execution")

    # run the test case
    runner.run(test)
    file_result.close()
copy code
copy code

 

After running the test script, I found an error:

File "C:\Python34\lib\HTMLTestRunner.py", line 642, in sortResult

if not rmap.has_key(cls):

So go to line 642 and modify the code:

5

Continue to report errors after running:

AttributeError: 'str' object has no attribute 'decode'

Go to lines 766, 772 and continue to modify (note: line 766 is uo and line 772 is ue, I was blind at the time, didn't notice these, thought it was the same, which led to some inexplicable errors reported, and it took a long time to toss):

6

After modification, it is found that an error is reported:

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'

Go to 631 to check and find the only print in the whole program:

print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime

This is the writing method of 2.x, let's modify it to the print of 3.x, and modify it as follows:

print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256944&siteId=291194637