Python3 HTMLTestRunner兼容中文

在Python3环境下,引用HTMLTestRunner生成测试报告,report_title传了中文,结果生成的报告中文是乱码:
这里写图片描述

解决方法直接拖到文末


港真,看到这个还挺崩溃的,因为我用的是Python3!!
调了一下代码,发现了问题,在HTMLTestRunner/result.py中有这样的代码:

    def generate_file(self, output, report_name, report):
        """ Generate the report file in the given path. """
        ......
        with open(path_file, 'w') as report_file:
            report_file.write(report)

写文件那行,在python3里应该指定编码方式,否则中文就是乱码。


解决方法:
修改..\Lib\site-packages\HtmlTestRunner\result.py文件中的generate_file方法,在写文件时添加encoding方式,改成这样就可以了

    def generate_file(self, output, report_name, report):
        """ Generate the report file in the given path. """
        ......
        with open(path_file, 'w', encoding='utf-8') as report_file:
            report_file.write(report)

献出结果:
这里写图片描述

HTMLTestRunner源码:https://github.com/oldani/HtmlTestRunner

猜你喜欢

转载自blog.csdn.net/u010895119/article/details/79364630
今日推荐