pytest自定義動態添加描述信息


先上效果圖:

 

修改pytest-html報告,分三部分.

pytest執行目錄新建conftest.py文件

import pytest
from py._xmlgen import html
from datetime import datetime
"""
Summary部分在此設置
"""
@pytest.mark.optionalhook
def pytest_html_results_summary(prefix, summary, postfix):
    #Get configure content.
    prefix.extend([html.p("測試人: 測試組")])



"""
Environment部分在此設置
"""
def pytest_configure(config):
    
    config._metadata['測試地址'] = xxxxxxxx#


"""
Results部分在此設置.
"""
@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.insert(2, html.th('Description'))
    cells.insert(3, html.th('Time', class_='sortable time', col='time'))
    # cells.insert(1,html.th("Test_nodeid"))
    cells.pop()

@pytest.mark.optionalhook
def pytest_html_results_table_row(report, cells):
    cells.insert(2, html.td(report.description))
    cells.insert(3, html.td(datetime.utcnow(), class_='col-time'))
    # cells.insert(1,html.td(report.nodeid))
    cells.pop()

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")   #設置編碼顯示中文

 

下面說一下怎么樣動態更改描述部分:

pytest-html默認獲取的是測試方法的__doc__屬性,也就是,測試函數下的注釋   如下的"""  """中的內容.

    def data(self, request):
        """
        fixture parameters.
        """

 

要動態傳參__doc__內容也是可以的.可以通過__doc__動態修改描述.

普通方法:   方法名.__doc__='fixture parameters.'

實例方法:   self.方法名.__func__.__doc__='fixture parameters.'     實例方法必須加__func__否則是只讀的.

class TestCaseExecution(object):
    """
    Use the python pytest framework.
    """
    def setup_class(self):
        pass
    

    def teardown_class(self):
        pass


    param_list = load_case_data()

    
    @pytest.fixture(scope='session', params=param_list)
    def data(self, request):
        """
        fixture parameters.
        """
        return request.param
    
    def testcase(self, data):
        self.testcase.__func__.__doc__ = data[0]['Desc']
        #Execution the YAML test case.
        exec_test_case(data)
        

 

使用此方法,動態傳入描述.

self.testcase.__func__.__doc__ = data[0]['Desc']

 


免責聲明!

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



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