pytest文档80 - 内置 fixtures 之 cache 写入中文显示\u4e2d\u6587问题(用打补丁方式解决)
前言
pytest 内置 fixtures 之 cache 写入中文的时候会在文件中写入\u4e2d\u6587
这种unicode编码格式。
如果想在文件中显示正常的中文,需重新Cache类的set方法来解决
问题描述
pytest 用例 cache 的使用参考前面这篇https://www.cnblogs.com/yoyoketang/p/15747082.html
当cache设置中文的时候,写入cache文件中文显示
问题原因
出现这个问题的根本原因是python3 的json库留下来的一个坑,先看以下简单的实例
import jsona = '上海-悠悠'
print(json.dumps(a))# ensure_ascii=False
print(json.dumps(a, ensure_ascii=False))
运行结果
"\u4e0a\u6d77-\u60a0\u60a0"
"上海-悠悠"
使用json调用dumps方法的时候默认ensure_ascii参数为True
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 方法实现
def set(self, key: str, value: object) -> None:"""Save value for the given key.:param key:Must be a ``/`` separated value. Usually the firstname 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 = Trueelse: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)returnif 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参数
data = json.dumps(value, indent=2, sort_keys=True)
问题原因找到了,接下来打个补丁,重写set方法即可
打补丁
以下这段补丁代码加到运行用例之前,放到项目根目录conftest.py文件的开始位置即可
from _pytest.cacheprovider import Cache
import jsondef new_set(self, key: str, value: object) -> None:"""Save value for the given key.:param key:Must be a ``/`` separated value. Usually the firstname 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 = Trueelse: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)returnif 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个地方
# 添加一个参数 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')
pytest文档80 - 内置 fixtures 之 cache 写入中文显示\u4e2d\u6587问题(用打补丁方式解决)相关推荐
- Django 2.1.3 文档-模板-内置标签和过滤器(tagfilter)
内置标签和过滤器 1.内置标签 一览 autoescape block comment csrf_token cycle debug extends filter(标签filter而不是过滤器filt ...
- 电脑上计算机中的文档怎么变成格子样,电脑有的软件文字显示小方格是怎么回事?怎样解决?...
2009-07-02 回答 你好啊.你说就是有的软件方格..其他的桌面什么的不方格是吗??那样的话问题比一般的方格复杂点.. 下面是给你找的资料.. 希望你能解决哦. 汉字乱码现象大致可分成四类:网页 ...
- 【pytest】内置 fixtures 之 tmpdir:创建临时文件
内置 fixtures 之 tmpdir: tmpdir 的作用是:在本地生成临时文件夹,并返回文件对象: 代码演示: 先写个测试用例,调用 tmpdir,执行一下看看: class TestDemo ...
- pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)
前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...
- pytest文档3-pycharm运行pytest
前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...
- wps批量将文档括号内的字符串修改颜色
wps批量将文档括号内的字符串修改颜色 文档内容如下,我们想把括号内的字符串全部修改为红色. 1)使用快捷键Ctrl+h,打开替换会话框. 输入查找的内容:((*)) 输出替换的内容:(\1) 然后点 ...
- PPT保存pps演示文档时,在另一个电脑中字体显示不正常!(已解决)
PPT保存pps演示文档时,在另一个电脑中字体显示不正常!(已解决)
- 【Unity3D读取数据】(一)Txt文档操作(创建、读取、写入、修改)
推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 QQ群:1040082875 大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有 ...
- word文档的左侧目录被隐藏了,怎么显示?
word文档的左侧目录被隐藏了,怎么显示? 解决方法: 注意:某title要在左侧目录现实就得设置为标题先. 视图-->http://www.yayihouse.com/yayishuwu/ch ...
最新文章
- 模拟打字机,文字逐个出现
- “厌氧动物”出现,科学家不清楚代谢方式,或颠覆对生命的认知
- linux平台IO多路复用 select接口使用例子
- LSMW批处理使用方法(06)_步骤4、5
- 【数据竞赛】时间序列竞赛炸榜技巧
- ora-01591:锁被未分布式事物处理/Distrib tran
- P1972 [SDOI2009]HH的项链
- Linux中自带正则表达式应用举例
- layui 怎么设置点击图片放大_layui等比例放大/缩小图片
- python中加减乘除是什么数据类型_python中,数字类型计算
- go语言 doc转换html,go语言使用wkhtmltopdf工具将html转成pdf
- 【CLR】解析AppDomain
- 第五十六题(最长公共子串)
- mfc多文档获取版本号_管理Microsoft Office文档—基于SOLIDWORKS PDM
- 如何能顺利往表中允许为null的字段插入null值(参数绑定)
- 【OBS-studio-webrtc】obs推流到webrtc服务器端:接管OBS输出流到自定义推流服务
- 微信小程序自定义tabbar 图标凸出效果
- Vue2.0源码解析 - 知其然知其所以然之keep-alive原理分析(一)
- word排版的时候如何将论文表格逆时针旋转90度竖放
- 什么是 PHP 过滤器?为什么使用过滤器?