Mac系统-Pytest-html-中文乱码(selenium+Pytest)

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
          
#再次运行你的自动化代码看看,就可以发现中文乱码问题已经解决了。

在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值