关于pytest中用例名称使用中文乱码的解决

场景:使用@pytest.mark.parametrize装饰器为用例自定义名称时,运行显示乱码。如下图所示:

解决方案:

1.在根目录 pytest.ini中增加一行代码

复制代码
[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support=True

2.在conftest.py中加入钩子函数

复制代码
def pytest_collection_modifyitems(items):
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")

3.采用英文名

相关推荐
姚青&3 天前
PyCharm 配置与界面化运行
pytest
姚青&3 天前
Pytest 测试用例断言
测试用例·pytest
姚青&3 天前
Pytest 命名规则
pytest
姚青&3 天前
Pytest 测试用例结构
测试用例·pytest
姚青&3 天前
Pytest 简介、安装与环境准备
pytest
测试渣4 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
小凯123455 天前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极5 天前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest