pytest dynamically add a custom description

First on renderings:

 

Modify pytest-html report, divided into three parts.

pytest implementation of the new directory file conftest.py

Import pytest
 from py._xmlgen Import HTML
 from datetime Import datetime
 "" " 
the Summary section in this setting 
" "" 
@ pytest.mark.optionalhook 
DEF pytest_html_results_summary (prefix, the Summary, postfix):
     # . the Get the configure Content 
    prefix.extend ([HTML .p ( " the test person: test group " )]) 



"" " 
Environment in this section provided 
" "" 
DEF pytest_configure (config): 
    
    config._metadata [ ' test address ' ] = XXXXXXXX # 


"" "
Results section in this setting. 
"" "
@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")   #设置编码显示中文

 

Said the following about how to dynamically change the description section:

pytest-html __doc__ default attribute is acquired test method, i.e., the note under test as a function of the content of "" "" in "".

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

 

__Doc__ dynamic content to pass parameters are also possible. __Doc__ description can be dynamically modified.

Common methods: Method name .__ doc __ = 'fixture parameters.'

Examples of the method:. Self method name. __Func__ .__ DOC = __ 'Parameters fixtures.' Examples of the method must be added to otherwise __func__ is read-only.

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)
        

 

Using this method, a dynamic passing description.

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

 

Guess you like

Origin www.cnblogs.com/yhleng/p/11225638.html