Python3安装HTMLTestRunner,修改HTMLTestRunner.py文件

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

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

下面更改内容转载自博客园-微微笑

 

1. 修改的地方

 

问题一: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, '\nTime Elapsed: %s' % (self.stopTime-self.startTime), 可改成 print('\nTime 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放置当前的项目文件夹下。

转至:https://blog.csdn.net/chailyn_feng/article/details/78177537

posted @ 2018-07-04 10:28  风吹稻香  阅读(3154)  评论(0编辑  收藏  举报