下载HTMLTestRunner并修改

目录

一. 下载HTMLTestRunner

二. 修改HTMLTestRunner

1. 修改内容

2. 修改原因


一. 下载HTMLTestRunner

下载报告模板地址:http://tungwaiyip.info/software/HTMLTestRunner.html

下载模块:

二. 修改HTMLTestRunner

将修改后的模块放到python安装目录下的../Python37路径Lib目录下 

下载的HTMLTestRunner.py是针对python2写的,所以针对python3需要适当更改其内容

1. 修改内容

第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:

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

第766行,将uo = o.decode('latin-1')修改成uo = e

第775行,将ue = e.decode('latin-1')修改成ue = e

2. 修改原因

问题一:No module named StringIO

原因:python 3 中 没有 StringIO 这个模块。这里我们需要使用io 这个模块来代替。

解决方法:

第94行引入的名称要改,从 import StringIO 改成import io。

相应的,539行 self.outputBuffer = StringIO.StringIO() 要改成self.outputBuffer = io.BytesIO()


问题二:AttributeError: 'dict' object has no attribute 'has_key'

原因:python 3 字典类型的object 已经不支持 has_key函数,我们需要使用in 来进行遍历。

解决方法:

定位到642行,if not rmap.has_key(cls): 需要换成 if not cls in rmap:


问题三:'str' object has no attribute 'decode'

原因:python3 里面对字符的操作中,decode已经拿掉了。

解决方法:

定位到772行,把 ue = e.decode('latin-1') 直接改成 ue = e 。

另外766还有类似的uo = o.decode('latin-1'),改成 uo=o ;


问题四 :TypeError: can't concat bytes to str

原因:定位一下,报在了778行的内容escape(uo+ue) 。这是因为我们上面给uo赋值的时候,走的是else流程,uo被赋值的是bytes类型的值。 而bytes类型不能直接转化为str类型。所以我们需要在前面给uo赋值的时候先将bytes类型转换为 str类型。

解决方法:

修改768行的 uo = o ,直接改成 uo = o.decode('utf-8') 。

另外 774还有类似的 ue = e, 改成 ue = e.decode('utf-8')。

问题五:TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'RPCProxy'

原因: python3 不支持 print >> sys.stderr 这种写法,这里定义输出流的话,采用print("This is print str",file=sys.stderr) 这种方式。

解决方法:

定位到631行,把print的语句修改掉,原来是print >>sys.stderr, ' Time Elapsed: %s' % (self.stopTime-self.startTime), 可改成 print(' Time Elapsed: %s' % (self.stopTime-self.startTime),file=sys.stderr)

问题六:TypeError: 'str' does not support the buffer interface

原因:定位一下,问题出在118行,这里s是str类型,我们需要把传过来的s转化为bytes类型。

解决方法:

定位到118行,把 self.fp.write(s) 修改为 self.fp.write(bytes(s,'UTF-8')) 即可。


看了很多文章都说把HTMLTestRunner.py文件放置在python35下的lib文件夹下,结果我试了很多次,也没有把HTMLTestRunner导入,运行总是报错说没有导入这个模块。

解决方法:把HTMLTestRunner.py放置当前的项目文件夹下。


 以下是我收集到的比较好的学习教程资源,虽然不是什么很值钱的东西,如果你刚好需要,可以评论区,留言【777】直接拿走就好了

各位想获取资料的朋友请点赞 + 评论 + 收藏,三连!

三连之后我会在评论区挨个私信发给你们~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
导入HTMLTestRunner到Python,生成测试报告的工具类 """ A TestRunner for use with the Python unit testing framework. It generates a HTML report to show the result at a glance. The simplest way to use this is to invoke its main method. E.g. import unittest import HTMLTestRunner ... define your tests ... if __name__ == '__main__': HTMLTestRunner.main() For more customization options, instantiates a HTMLTestRunner object. HTMLTestRunner is a counterpart to unittest's TextTestRunner. E.g. # output to a file fp = file('my_report.html', 'wb') runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='My unit test', description='This demonstrates the report output by HTMLTestRunner.' ) # Use an external stylesheet. # See the Template_mixin class for more customizable options runner.STYLESHEET_TMPL = '<link rel="stylesheet" href="my_stylesheet.css" type="text/css">' # run the test runner.run(my_test_suite) ------------------------------------------------------------------------ Copyright (c) 2004-2007, Wai Yip Tung All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Wai Yip Tung nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值