Mac系统-Pytest-html-中文乱码(selenium+Pytest)
在自动化测试中,使用Pytest模块生成测试报告时,中文显示乱码该如何解决?
问题描述
在自动化测试中,使用Pytest生成html报告时,打开html文件后,中文显示乱码
# 原因分析:
> class TestResult:
def __init__(self, outcome, report, logfile, config):
self.test_id = report.nodeid.encode("utf-8").decode("unicode_escape")
这里的代码最后已经将编码格式设置为了UTF-8,后面又跟了一个decode("unicode_escape")进行了二次解码
# 解决方案:
#在系统李找到名叫:plugin.py的文件进行更改
#1、进入终端,查看下当前python的路径
which Python3
#2、拿到路径如下:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
#3、这时我们只要这一段,复制这段路径,去访达按键盘 commmand+shift+G
/Library/Frameworks/Python.framework/Versions/3.10
#4、进入后在进入到:/lib/python3.10/site-packages/pytest_html/
lib/python3.10/site-packages/pytest_html/plugin.py
#4.1绝对路径(根据自己的python版本自行更改)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pytest_html/plugin.py
#5、更改plugin.py(大概在162行,可使用VScode进行编辑)
#更改前:
class TestResult:
def __init__(self, outcome, report, logfile, config):
self.test_id = report.nodeid.encode("utf-8").decode("unicode_escape")
#更改后:建议把self.test_id = report.nodeid.encode("utf-8").decode("unicode_escape")注释掉不要删掉,避免引起其他错误
#class TestResult:
def __init__(self, outcome, report, logfile, config):
# 子鞍修改方法,解决乱码问题
#self.test_id = report.nodeid.encode("utf-8").decode("unicode_escape")
self.test_id = report.nodeid
#再次运行你的自动化代码看看,就可以发现中文乱码问题已经解决了。