HTMLTestRunner修改成Python3版本

        在拜读虫师大神的Selenium2+Python2.7时,发现生成HTMLTestRunner的测试报告使用的HTMLTestRunner的模块 是用的Python2的语法。而我本人比较习惯与Python3。而且自己也是用的Python3.4的环境,在网上找了很多资料,修改了下 HTMLTestRunner.py

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

下载地址:http://tungwaiyip.info/software/HTMLTestRunner.html

修改后下载地址:http://pan.baidu.com/s/1tp3Ts

 

修改汇总:

第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模块报错。

1

在HTMLTestRunner的94行中,是使用的StringIO,但是Python3中,已经没有StringIO了。取而代之的是io.StringIO。所以将此行修改成import io

2

在HTMLTestRunner的539行中,self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()

3

修改以后,成功引入模块了

4

 

执行脚本代码:

复制代码

# -*- 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):
    #初始化设置
    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"用例执行情况")

    #运行测试用例
    runner.run(test)
    file_result.close()

复制代码

 

运行测试脚本后,发现报错:

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

if not rmap.has_key(cls):

所以前往642行修改代码:

5

运行后继续报错:

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

前往766, 772行继续修改(注意:766行是uo而772行是ue,当时眼瞎,没有注意到这些,以为是一样的,导致报了一些莫名其妙的错误,折腾的半天):

6

修改后运行,发现又报错:

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查看,发现整个程序中,唯一一个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))

7

继续运行脚本,OK运行成功

8

查看指定的目录生成了result.html

9

点击打开报告:

image

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.将 HTMLTestRunner.py 放置在 C:\Python36\Lib 下 2.涉及到创建目录和时间,需要在脚本开头 import os import time 3.执行脚本中删除语句 unittest.main() ,一般在脚本最后,然后添加如下语句: #导入HTMLTestRunner库,这句也可以放在脚本开头 from HTMLTestRunner import HTMLTestRunner #定义脚本标题,加u为了防止中文乱码 report_title = u'登陆模块测试报告' #定义脚本内容,加u为了防止中文乱码 desc = u'手机JPG登陆模块测试报告详情:' #定义date为日期,time为时间 date=time.strftime("%Y%m%d") time=time.strftime("%Y%m%d%H%M%S") #定义path为文件路径,目录级别,可根据实际情况自定义修改 path= 'D:/Python_test/'+ date +"/login/"+time+"/" #定义报告文件路径和名字,路径为前面定义的path,名字为report(可自定义),格式为.html report_path = path+"report.html" #判断是否定义的路径目录存在,不能存在则创建 if not os.path.exists(path): os.makedirs(path) else: pass #定义一个测试容器 testsuite = unittest.TestSuite() #将测试用例添加到容器 testsuite.addTest(测试类名("测试方法名1")) testsuite.addTest(测试类名("测试方法名2")) #将运行结果保存到report,名字为定义的路径和文件名,运行脚本 with open(report_path, 'wb') as report: runner = HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(testsuite) #关闭report,脚本结束 report.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值