Search code examples
pytestpytest-html

How to add mark in the pytest-html report


I'm trying to read the source code of pytest-html, do not know it how to get results.. if i add a mark like @pytest.mark.p0, i want to show this mark in the pytest-html report, like add description with official demo, how to add it.


Solution

  • i resolve it myself. like description

    @pytest.mark.optionalhook
    def pytest_html_results_table_header(cells):
    #<th class="sortable result initial-sort asc inactive" col="result"><div class="sort-icon">vvv</div>Result</th>
    cells.insert(1, html.th('Description'))
    cells.insert(2, html.th('Priority', class_="sortable", col="priority"))
    cells.insert(3, html.th('Owner', class_="sortable", col="owner"))
    cells.pop()
    
    @pytest.mark.optionalhook
    def pytest_html_results_table_row(report, cells):
    cells.insert(1, html.td(report.description))
    cells.insert(2, html.td(report.priority))
    cells.insert(3, html.td(report.owner))
    cells.pop()
    
    @pytest.mark.hookwrapper
    def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)
    
    marker_priority = item.get_closest_marker("priority")
    if marker_priority:
        report.priority = marker_priority.kwargs['value']
        #print(marker_priority)
    
    marker_owner = item.get_closest_marker("owner")
    if marker_owner:
        report.owner = marker_owner.kwargs['name']