Python unittest to produce a visible report that requires the help of a third party package
Download htmltestrunner.py Third Party library, reference address: http://tungwaiyip.info/software/HTMLTestRunner.html
is a cow himself wrote, really pretty good, admire admire
After downloading, the files are copied to the Python project directory and can be referenced to the line
Use:
The code is as follows |
Copy Code |
Import Lib. Htmltestrunner From Testdatadriver import Testdriver Import time Import Sys,os Reload (SYS) Sys.setdefaultencoding (' Utf-8 ')
Def htr (): Runner = lib. Htmltestrunner Runner.run (Suite) #自动进行测试 |
If you need to specify the name and path of the report output, you can do so in the following ways:
The code is as follows |
Copy Code |
Def htr (): #使用HTMLTestRunner配置参数, output report path, report title, description Runner = lib. Htmltestrunner.htmltestrunner (stream=fp,title= ' Api_test_ ' +str (localtimes), description= ' Report_description ') Runner.run (Suite) #自动进行测试 |
Report shows how to solve the garbled problem in Chinese
The output of the report may contain Chinese, you need to determine how the htmltestrunner.py source file encoding
First confirm that you set the encoding in the code file referencing Htmltestrunner
The code is as follows |
Copy Code |
Import Sys Reload (SYS) Sys.setdefaultencoding (' Utf-8 ') |
Open the htmltestrunner.py source file and find the following
The code is as follows |
Copy Code |
# O and E should be-byte string because they are collected from stdout and stderr? If Isinstance (O,STR): # todo:some problem with ' string_escape ': it escape N and mess up formating # UO = Unicode (O.encode (' String_escape ')) #uo = O.decode (' latin-1 ') Else UO = O If Isinstance (E,STR): # todo:some problem with ' string_escape ': it escape N and mess up formating # UE = Unicode (E.encode (' String_escape ')) #ue = E.decode (' latin-1 ') Else UE = E Add a Utf-8 decoding # O and E should be-byte string because they are collected from stdout and stderr? If Isinstance (O,STR): # todo:some problem with ' string_escape ': it escape N and mess up formating # UO = Unicode (O.encode (' String_escape ')) #uo = O.decode (' latin-1 ') UO = O.decode (' Utf-8 ') Else UO = O If Isinstance (E,STR): # todo:some problem with ' string_escape ': it escape N and mess up formating # UE = Unicode (E.encode (' String_escape ')) #ue = E.decode (' latin-1 ') UE = E.decode (' Utf-8 ') Else UE = E |
OK, as described above, Chinese garbled problem resolution, it works