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))

再次運行

 

 

非常好,已經可以正常顯示用例注釋名了.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM