python - 带有西里尔符号的 pytest 3.0.5

标签 python pytest python-unicode

我在 pytest-3.0.5 的文件路径中显示带有俄文文本的测试名称时遇到问题。 当我在 pytest==2.9.2 上运行时 - 一切正常:

py.test -s -q --collect-only

输出:

test_card.py::test_graphic_card[/root/test_cases/files/Кредитная_карта_мир_41.png]

但是当我尝试在 pytest==3.0.5 上运行相同的命令时:

py.test -s -q --collect-only

输出:

test_card.py::test_graphic_card[/root/test_cases/files/\xd0\x9a\xd1\x80\xd0\xb5\xd0\xb4\xd0\xb8\xd1\x82\xd0\xbd\xd0\xb0\xd1\x8f_\xd0\xba\xd0\xb0\xd1\x80\xd1\x82\xd0\xb0_\xd0\xbc\xd0\xb8\xd1\x80_41.png]

我应该怎么做才能让 py.test==3.0.5 像显示的 py.test==2.9.2 一样显示测试名称?

最佳答案

我查看了 pytest 源代码,发现在最新版本的 pytest 中 收集测试时,它们会使用最近添加的函数“_escape_strings”进行处理。 在这个函数中:

if isinstance(val, bytes):
    try:
        return val.encode('ascii')
    except UnicodeDecodeError:                     
        return val.encode('string-escape')
    else:
        return val.encode('unicode-escape')

我想这个函数是不言自明的。 最后,我在最终测试报告中解决了我的解码和结果转义问题,例如:

result = "/root/test_cases/files/\xd0\x9a\xd1\x80\xd0\xb5\xd0\xb4\xd0\xb8\xd1\x82\xd0\xbd\xd0\xb0\xd1\x8f_\xd0\xba\xd0\xb0\xd1\x80\xd1\x82\xd0\xb0_\xd0\xbc\xd0\xb8\xd1\x80_41.png"
print result.decode('string-escape') 
Out: /root/test_cases/files/Кредитная_карта_мир_41.png

关于python - 带有西里尔符号的 pytest 3.0.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41334111/

相关文章:

java - python客户端和java服务器之间的通信

python - SQLAlchemy 在测试期间恢复 auto_increment (pytest)

file-io - 如何在 python 3.3.3 中读取文本文件并将其存储在变量中?

python - 代码上周有效 - UnicodeEncodeError : 'ascii' codec can't encode characters - Python 3. X

Python请求400错误

python - 将 Z3Py 与 Python 3.3 结合使用

python - 在 python 中迭代一个漂亮的 soup 数组

git - pytest 仅在推送时使用预提交 Hook 进行测试,而不是在提交时进行测试

python - 按顺序执行pytest

python - Unicode 格式