关于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.采用英文名

相关推荐
shao9185162 天前
Gradio全解14——使用Gradio构建MCP的服务器与客户端(4)——Python包命令:uv与uvx实战
pytest·uv·1024程序员节·npx·uvx·uv pip·ruff
我的xiaodoujiao3 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 20--PO(POM) 设计模式和用例撰写
python·学习·测试工具·设计模式·pytest
sky0Lan3 天前
一个类似 pytest 的 html 报告
android·html·pytest
一半烟火以谋生4 天前
Python + Pytest + Allure 自动化测试报告教程
开发语言·python·pytest
一半烟火以谋生5 天前
pytest conftest.py 使用教程
pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 19--测试框架Pytest基础 3--前后置操作应用
python·学习·测试工具·pytest
鱼鱼说测试6 天前
pytest+yaml+allure接口自动化测试框架
pytest
ThreeAu.8 天前
pytest 实战:用例管理、插件技巧、断言详解
python·单元测试·pytest·测试开发工程师
我的xiaodoujiao9 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 18--测试框架Pytest基础 2--插件和参数化
python·学习·测试工具·pytest
小小测试开发9 天前
pytest 库用法示例:Python 测试框架的高效实践
开发语言·python·pytest