前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >pytest文档80 - cache 写入中文显示\u4e2d\u6587问题(打补丁解决)

pytest文档80 - cache 写入中文显示\u4e2d\u6587问题(打补丁解决)

作者头像
上海-悠悠
发布2022-01-10 08:28:53
4580
发布2022-01-10 08:28:53
举报

前言

pytest 内置 fixtures 之 cache 写入中文的时候会在文件中写入\u4e2d\u6587 这种unicode编码格式。 如果想在文件中显示正常的中文,需重新Cache类的set方法来解决

问题描述

pytest 用例 cache 的使用参考前面这篇https://www.cnblogs.com/yoyoketang/p/15747082.html 当cache设置中文的时候,写入cache文件中文显示

问题原因

出现这个问题的根本原因是python3 的json库留下来的一个坑,先看以下简单的实例

代码语言:javascript
复制
import json

a = '上海-悠悠'
print(json.dumps(a))

# ensure_ascii=False
print(json.dumps(a, ensure_ascii=False))

运行结果

代码语言:javascript
复制
"\u4e0a\u6d77-\u60a0\u60a0"
"上海-悠悠"

使用json调用dumps方法的时候默认ensure_ascii参数为True

代码语言:javascript
复制
def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True,
        allow_nan=True, cls=None, indent=None, separators=None,
        default=None, sort_keys=False, **kw):

所以会导致中文在转json的时候,是以\u4e0a\u6d77这种编码格式显示的.

Cache类的set方法

pytest 内置 fixtures 之 cache 是 Cache 类的实例,看下 Cache 类的 set 方法实现

代码语言:javascript
复制
def set(self, key: str, value: object) -> None:
        """Save value for the given key.

        :param key:
            Must be a ``/`` separated value. Usually the first
            name is the name of your plugin or your application.
        :param value:
            Must be of any combination of basic python types,
            including nested types like lists of dictionaries.
        """
        path = self._getvaluepath(key)
        try:
            if path.parent.is_dir():
                cache_dir_exists_already = True
            else:
                cache_dir_exists_already = self._cachedir.exists()
                path.parent.mkdir(exist_ok=True, parents=True)
        except OSError:
            self.warn("could not create cache path {path}", path=path, _ispytest=True)
            return
        if not cache_dir_exists_already:
            self._ensure_supporting_files()
        data = json.dumps(value, indent=2, sort_keys=True)
        try:
            f = path.open("w")
        except OSError:
            self.warn("cache could not write path {path}", path=path, _ispytest=True)
        else:
            with f:
                f.write(data)

这里面有一句json.dumps没有传ensure_ascii参数

代码语言:javascript
复制
data = json.dumps(value, indent=2, sort_keys=True)

问题原因找到了,接下来打个补丁,重写set方法即可

打补丁

以下这段补丁代码加到运行用例之前,放到项目根目录conftest.py文件的开始位置即可

代码语言:javascript
复制
from _pytest.cacheprovider import Cache
import json

def new_set(self, key: str, value: object) -> None:
    """Save value for the given key.

    :param key:
        Must be a ``/`` separated value. Usually the first
        name is the name of your plugin or your application.
    :param value:
        Must be of any combination of basic python types,
        including nested types like lists of dictionaries.
    """
    path = self._getvaluepath(key)
    try:
        if path.parent.is_dir():
            cache_dir_exists_already = True
        else:
            cache_dir_exists_already = self._cachedir.exists()
            path.parent.mkdir(exist_ok=True, parents=True)
    except OSError:
        self.warn("could not create cache path {path}", path=path, _ispytest=True)
        return
    if not cache_dir_exists_already:
        self._ensure_supporting_files()
    data = json.dumps(value, ensure_ascii=False, indent=2, sort_keys=True)
    try:
        f = path.open("w", encoding='utf-8')
    except OSError:
        self.warn("cache could not write path {path}", path=path, _ispytest=True)
    else:
        with f:
            f.write(data)

Cache.set = new_set

主要改动2个地方

代码语言:javascript
复制
# 添加一个参数 ensure_ascii=False
data = json.dumps(value, ensure_ascii=False, indent=2, sort_keys=True)
# 添加一个参数 encoding='utf-8'
f = path.open("w", encoding='utf-8')
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 问题描述
  • 问题原因
  • Cache类的set方法
  • 打补丁
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档