首页 > 编程 > Python > 正文

python使用 HTMLTestRunner.py生成测试报告

2020-02-16 10:25:54
字体:
来源:转载
供稿:网友

本文介绍了python使用 HTMLTestRunner.py生成测试报告 ,分享给大家,具体如下:

HTMLTestRunner.py python 2版本

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

使用时,先建立一个”PyDev Package“,将下载下来的HTMLTestRunner.py文件拷贝在该目录下。

例子:testcase5_dynamic.py

import unittestfrom dev.widget import Widgetclass WidgetTestCase(unittest.TestCase): def setUp(self):  self.widget=Widget()   def tearDown(self):  self.widget.dispose()  self.widget=None   def testSize(self):  self.assertEqual(self.widget.getSize(), (40,40), "Wrong")   def testResize(self):  self.widget.resize(100, 100)  self.assertEqual(self.widget.getSize(), (100,100), "Wrong")

html_report.py:

#coding:utf-8from lib import HTMLTestRunnerimport unittestfrom testcase5_dynamic import WidgetTestCaseif __name__=='__main__': suite=unittest.makeSuite(WidgetTestCase) filename='D://myreport.html' fp=file(filename,'wb') runner=HTMLTestRunner.HTMLTestRunner(fp,title=u'my unit test',description=u'This is a report test') runner.run(suite)

Run的时候,需要使用Python Run,使用Python unit-test跑测试生成不了myreport.html,目前还不知道为什么。

有时候,不会立即生成D://myreport.html,我们可以自己先建立一个空的myreport.html,这样再运行之后打开就会看到报告内容。

 

HTMLTestRunner.py 的python3 版本

由于 HTMLTestRunner.py 原本就是python2版本,目前还没找到python3版本,所以需要我们自己修改 HTMLTestRunner.py 文件。

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类型。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表