python pytest测试框架介绍七 ---测试用例显示用例注释名称

在unittest中,用例执行过程后,会显示注释中的名称,但在pytest中没有显示,只显示函数名,如下:

 

 如果下面的代码:

class Test_Bbb(unittest.TestCase):
    """临时"""

    def test_1(self):
        """测试test1"""
        log.info("afjdfdjlajfd")
        assert 5==6

    def test_2(self):
        """测试test2"""
        log.info("afjdfdjlajfd")
        assert 5==6

用pytest执行,想显示中文怎么办呢?看了下官方的,没有找到对应的参数,有一个第3方插件pytest-spec,感觉也不是我想要的,查之,有一个解决方法

在conftest.py中写入下面代码

def pytest_itemcollected(item):
    par = item.parent.obj
    node = item.obj
    pref = par.__doc__.strip() if par.__doc__ else par.__class__.__name__
    suf = node.__doc__.strip() if node.__doc__ else node.__name__
    if pref or suf:
        item._nodeid = ' '.join((pref, suf))

再次运行

 

 

非常好,已经可以正常显示用例注释名了.

posted @ 2020-06-19 16:08  Believer007  阅读(3128)  评论(3编辑  收藏  举报