记录(1):python 3.x安装HTMLTestRunner模块及简单使用
一、pip安装 (失败)
- 命令
pip install html-testRunner
- cmd中显示安装成功,安装的路径为
D:\software\Anaconda\Anaconda\Lib\site-packages
- 但运行时,
runner = HtmlTestRunner.HTMLTestRunner(output=filepath)
会提示:
AttributeError: module ‘HTMLTestRunner’ has no attribute ‘HTMLTestRunner’
二、直接下载.py文件
-
下载后放入相应的Lib文件夹下即可:
-
import HTMLTestRunner
后提示 No module name ‘StringIO’,因为下载的HTMLTestRunner是python2.x版本的,要适应python3.x需要进行如下修改:
HTMLTestRunner.py:
(1) LIne94:import StringIO
改为import io
;
(2) LIne539:self.outputBuffer = StringIO.StringIO()
改为self.outputBuffer = io.StringIO()
;
(3) Line631:print>>sys.stderr, ‘\nTime Elapsed: %s‘ %(self.stopTime-self.startTime)
改为print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
(4) LIne642:if not rmap.has_key(cls):
改为if not cls in rmap:
;
(5) Line766:uo = o.decode('latin-1')
改为uo = e
;
(6) Line772:ue = e.decode('latin-1')
改为ue = e
; -
一切正常:
三、配合unittest的简单使用
#!/usr/bin/python
# -*- coding:utf-8 -*-
"""
FileName:Test.py
Author:LSD
Date:2021-10-08
"""
import unittest
import HTMLTestRunner
class testcase(unittest.TestCase):
def setUp(self):
pass
def test_case1(self):
# True
self.assertEqual(2 + 3 + 10, 15)
def test_case2(self):
# False
self.assertEqual(150 / 10, 160)
def test_case3(self):
# False
self.assertEqual(2 * 5 * 7, 40)
def test_case4(self):
# True
self.assertEqual(121 - 21, 100)
def tearDown(self):
pass
def suite():
suiteTest = unittest.TestSuite()
suiteTest.addTest(testcase("test_case1"))
suiteTest.addTest(testcase("test_case2"))
suiteTest.addTest(testcase("test_case3"))
suiteTest.addTest(testcase("test_case4"))
return suiteTest
if __name__ == "__main__":
# 测试报告存放路径(要具体到报告的文件名)
# 转义字符
filepath = 'D:\\Lesting\\python\\pyresult.html'
fp = open(filepath, 'wb')
# 定义测试报告的标题与描述
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u'Title', description=u'测试报告描述')
runner.run(suite())
fp.close()
执行结果:
Time Elapsed: 0:00:00
.FF.
Process finished with exit code 0
(通过两个,失败两个)
生成的报告 pyresult.html: