【问题描述】
在conftest.py中添加中文编码转换后(代码如下),测试用例中定义的测试名称为中文可以正常展示;从yml文件中读取的中文名称显示仍未乱码;请帮忙查看一下问题,谢谢!!!
conftest.py在根目录下,内容如下:
#修改测试用例名称中文编码,utf-8转换为unicode
def pytest_collection_modifyitems(
session: "Session", config: "Config", items: List["Item"]
) -> None:
for item in items:
item.name = item.name.encode('utf-8').decode('unicode-escape')
item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
测试数据calc.yml
add:
int:
datas:
- [ 1,1,2 ]
- [ 100,400,300 ]
- [ 1,0,1 ]
- [ -1,1,0 ]
ids: [ '整数','bigint','zero','minus' ]
测试脚本:
def getDatas():
with open("./data/calc.yml") as f:
datas = yaml.safe_load(f)
return datas
datas_int = getDatas().get("add").get("int").get("datas")
datas_ids = getDatas().get("add").get("int").get("ids")
@pytest.mark.parametrize('a,b,expect',datas_int,ids=datas_ids)
def test_add(self,a,b,expect,get_calc):
#实例化被测类
result = get_calc.add(a,b)
assert result == expect