How to solve the Chinese garbled characters of htmltestrunner output by python

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.