有时候我们做自动化测试时候,希望测试用例的名字是中文,方便查看。但是pytest现在的版本并不支持中文的测试用例名称显示。我们想办法修改源代码实现
遇到的问题
如果测试用例的参数中有中文,你看到的pytest的测试结果将是这样的:
test/ximalaya/album/test_category_list.py::TestCategoryList::test_category_list[[stg]\u83b7\u53d6\u559c\u9a6c\u62c9\u96c5\u5185\u5bb9\u5206\u7c7b] PASSED
解决问题
先说做法,参考这个PR:
https://github.com/pytest-dev/pytest/pull/4567/commits/13505b0ea20d26b1e656325e08a57bb46298c771
这个做法是将pytest的源码中的node.py(pytest>=3.0时是main.py)文件的Node类中的name属性做一下编码解码操作
# self.name = name
self.name = name.encode("utf-8").decode("unicode_escape") # 支持中文
这样修改后,效果是:
test/ximalaya/album/test_category_list.py::TestCategoryList::test_category_list[[stg]获取喜马拉雅内容分类] PASSED
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:湘潭市,area:雨湖区}] PASSED [ 43%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:乌鲁木齐市,area:沙依巴克区}] PASSED [ 43%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:南京市,area:六合区}] PASSED [ 44%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:连云港市,area:连云区}] PASSED [ 44%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:平顶山市,area:石龙区}] PASSED [ 44%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:柳州市,area:融安县}] PASSED [ 44%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:长治市,area:长子县}] PASSED [ 44%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:甘孜藏族自治州,area:德格县}] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:楚雄彝族自治州,area:双柏县}] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[上海市闵行区] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:渭南市,area:潼关县}] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:博尔塔拉蒙古自治州,area:温泉县}] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:泰安市,area:岱岳区}] PASSED [ 45%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:南京市,area:江宁区}] PASSED [ 46%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:株洲市,area:醴陵市}] PASSED [ 46%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:文山壮族苗族自治州,area:西畴县}] PASSED [ 46%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:宜春市,area:樟树市}] PASSED [ 46%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:铜陵市,area:铜官区}] PASSED [ 46%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:岳阳市,area:君山区}] PASSED [ 47%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[shaoguan] PASSED [ 47%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:临汾市,area:大宁县}] PASSED [ 47%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:佳木斯市,area:前进区}] PASSED [ 47%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:黑河市,area:孙吴县}] PASSED [ 47%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:舟山市,area:嵊泗县}] PASSED [ 48%]
test/weather/test_info_now.py::TestInfoNowCacheStrategy::test_info_now[{city:襄阳市,area:保康县}] PASSED [ 48%]
从上面的测试console中可以看到,[]里面的是中文了,这样看起来就爽多了。
不修改源码的解决办法
上面的办法缺点是需要需改源码,当升级后,需要重新修改源码。另一个办法是通过在conftest.py中实现pytest提供的HOOK函数pytest_collection_modifyitems,这样就不用修改pytest源码了。一劳永逸。
def pytest_collection_modifyitems(items):
"""
测试用例收集完成时,将收集到的item的name和nodeid的中文显示在控制台上
:return:
"""
for item in items:
item.name = item.name.encode("utf-8").decode("unicode_escape")
item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")