Python Unit Test - Test Report: HTMLTestRunner -1
Posted Rolei_zl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Unit Test - Test Report: HTMLTestRunner -1相关的知识,希望对你有一定的参考价值。
htmlTestRunner
1)http://tungwaiyip.info/software/HTMLTestRunner.html -- 网站提供的版本支持python2.7
2)HTMLTestRunner is an extension to the Python standard library's unittest module. It generates easy to use HTML test reports.
HTMLTestRunner扩展了python标准的单元测试库,方便生成HTML格式的测试报告。
3)HTMLTestRunner python2 转 python3
1. line 94, in <module> import StringIO
ModuleNotFoundError: No module named 'StringIO'
>> import StringIO -> import os
2. line 539
>> self.outputBuffer = StringIO.StringIO() -> self.outputBuffer = io.StringIO()
3. line 688, self.stream.write(output.encode('utf8'))
TypeError: descriptor 'encode' for 'str' objects doesn't apply to a 'bytes' object
>> self.stream.write(output.encode('utf8') ->
self.stream.write(output.encode('utf8').decode('utf8'))
4. line 632, in run print >>sys.stderr, '\\nTime Elapsed: %s' % (self.stopTime-self.startTime)
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and 'StdOutputFile'. Did you mean "print(<message>, file=<output_stream>)"?
>> print >>sys.stderr, '\\nTime Elapsed: %s' % (self.stopTime-self.startTime) -> print(sys.stderr, '\\nTime Elapsed: %s' % (self.stopTime-self.startTime))
5. line 645, in sortResult
if not rmap.has_key(cls): AttributeError: 'dict' object has no attribute 'has_key'
>> if not rmap.has_key(cls): -> if not cls in rmap:
# if run HTMLTestRunner directly, update followed line 6.
6. line 771, in _generate_report_test
uo = o.decode('latin-1') AttributeError: 'str' object has no attribute 'decode'
>> uo = o.decode('latin-1') -> uo = o.encode('utf-8').decode('utf-8')
7. line 778, in _generate_report_test
ue = e.decode('latin-1') AttributeError: 'str' object has no attribute 'decode'
>> ue = e.decode('latin-1') -> ue = o.encode('utf-8').decode('utf-8')
8. line 692, in generateReport
self.stream.write(output.encode('utf8').decode('utf8')) TypeError: a bytes-like object is required, not 'str'
>> 调用HTMLTestReport时
fp = file('my_report.html', 'wb') -> fp = file('my_report.html', 'w')
5)使用HTMLTestRunner
import unittest
import os
import HTMLTestRunner
def pyjiou(n):
if(n%2 == 0):
print("ou shu")
return "ou shu"
else:
print("ji shu")
return "ji shu"
def py10(n):
if(n < 10):
print("less 10")
return "less 10"
else:
print("big 10")
return "big 10"
'''
python unit test module, 2 class with 3 test method
'''
class functest(unittest.TestCase):
def setUp(self):
print("test start -- : ")
def test_ajiou(self):
print("method 1: atest_jiou")
self.assertEqual(pyjiou(4),"ou shu") #pass
# self.assertEqual(pyjiou(5),"ou shu") #fail
def test_bpy10(self):
print("method 2: btest_py10")
self.assertEqual(py10(4),"less 10") #pass
self.assertEqual(py10(4),"big 10") #fail
def test_cother(self):
print("method 3: ctest_other")
self.assertEqual('sup'.upper(),'SUP') #pass
self.assertTrue('sup'.isupper()) #fail
def tearDown(self):
print(" -- test end \\n")
class ft(unittest.TestCase):
def test_data(self):
print("method 4: test_data")
self.assertEqual(pyjiou(1),"ou shu") #fail
def suite_all(): # merge test methods into test suite
suite = unittest.TestSuite()
suite.addTest(functest('test_ajiou'))
suite.addTest(functest('test_bpy10'))
suite.addTest(functest('test_cother'))
suite.addTest(ft('test_data'))
return suite
if __name__ == "__main__":
## unittest.main() # call unit test
## runner = unittest.TextTestRunner() # call unit test suite
## runner.run(suite_all())
## HTMLTestRunner.main() # run HTMLTestRunner
# generate test result report and output into file
path = os.getcwd() + "/TestRport.html"
fp = open(path, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream=fp,
title='This unit test for python',
description='Python Unit报告生成使用HTMLTestRunner'
)
## runner.STYLESHEET_TMPL = '<link rel="stylesheet" href="my_stylesheet.css" type="text/css">'
runner.run(suite_all())
fp.close()
以上是关于Python Unit Test - Test Report: HTMLTestRunner -1的主要内容,如果未能解决你的问题,请参考以下文章
Python:如果存在值,则通过更新而不是覆盖来进行字典合并
[Python Test] Use pytest fixtures to reduce duplicated code across unit tests