pytest-html icon indicating copy to clipboard operation
pytest-html copied to clipboard

How to add additional column in pytest html report

Open Alfeshani-Kachhot opened this issue 3 years ago • 5 comments

I need help regarding pytest html report customization. I need to print failed network request status code(By TestCase wise) in report so I did the below code. StatusCode column created successfully but not getting data in html report. also, test case-wise statuscode row does not appear in the report.

Conftest.py

@pytest.mark.optionalhook
def pytest_html_results_table_header(cells):
    cells.append(html.th('Statuscode'))

@pytest.mark.optionalhook
def pytest_html_result_table_row(report,cells):
    cells.append(html.td(report.statuscode))

def pytest_runtest_makereport(item):
    """
    Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
    :param item:
    """
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    setattr(report, "duration_formatter", "%H:%M:%S.%f")
    extra = getattr(report, 'extra', [])
    statuscode = []


    if report.when == 'call' or report.when == "setup":
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            file_name = report.nodeid.replace("::", "_")+".png"
            _capture_screenshot(file_name)
            if file_name:
                html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % file_name
                extra.append(pytest_html.extras.html(html))
            
            for request in driver.requests:
                if url in request.url and request.response.status_code >=400 and request.response.status_code <= 512:
                            statuscode.append(request.response.status_code)
                        
        print("*********Status codes************",statuscode)
        report.statuscode=statuscode
        report.extra = extra

Alfeshani-Kachhot avatar Jul 19 '22 13:07 Alfeshani-Kachhot

With a little luck, it's due to a typo here: pytest_html_result_table_row

should be pytest_html_results_table_row

BeyondEvil avatar Jul 19 '22 14:07 BeyondEvil

@BeyondEvil

Yes, thank you for your help. but now I m getting below error. Can you please help? I tried but did not get much documentation help regarding customization of pytest html report.

line 51, in pytest_html_results_table_row cells.append(html.td(report.statuscode)) AttributeError: 'TestReport' object has no attribute 'statuscode'

Alfeshani-Kachhot avatar Jul 20 '22 04:07 Alfeshani-Kachhot

That's probably because the pytest_html_results_table_row hook is called before the pytest_runtest_makereport one.

BeyondEvil avatar Jul 20 '22 09:07 BeyondEvil

Yes, my pytest_runtest_makereport implemented with below but still pytest_html_results_table_row called before below method.

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item,call):

Alfeshani-Kachhot avatar Jul 20 '22 11:07 Alfeshani-Kachhot

Try adding tryfirst/trylast arguments to the decorators

BeyondEvil avatar Jul 20 '22 11:07 BeyondEvil

Closing this for lack of interaction. Feel free to re-open.

BeyondEvil avatar Mar 05 '23 16:03 BeyondEvil