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

4.0.0 - index Error thrown when customising the table

Open ramyavi opened this issue 2 years ago • 6 comments
trafficstars

Background Info : I have customised the columns in pytest_report to add more columns than the ones provided

Issue 1: with the latest version of the pytest_html 4.0.0 I replaced the following old code which uses py package(as it has been deprecated) with the new one (as per the doc). Also None of the changes, I'm making in this function pytest_html_results_table_header to have the column header added/changed is being taken in to account.

function name:pytest_html_results_table_row Old code: cells.insert(2, html.td(sourcedata)) cells.insert(3, html.td(expected_values))

New code: cells.insert(2, "

sourcedata") cells.insert(3, "expected_values")

But I'm getting the following error when trying to do this.

INTERNALERROR> Traceback (most recent call last): INTERNALERROR> File "C:\file_path\new\Lib\site-packages_pytest\main.py", line 270, in wrap_session INTERNALERROR> session.exitstatus = doit(config, session) or 0 INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages_pytest\main.py", line 324, in _main INTERNALERROR> config.hook.pytest_runtestloop(session=session) INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_hooks.py", line 493, in call INTERNALERROR> return self._hookexec(self.name, self._hookimpls, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_manager.py", line 115, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_callers.py", line 152, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_result.py", line 114, in get_result INTERNALERROR> raise exc.with_traceback(exc.traceback) INTERNALERROR> File "C:\filepath\new\Lib\site-packages\pluggy_callers.py", line 77, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages_pytest\main.py", line 349, in pytest_runtestloop INTERNALERROR> item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_hooks.py", line 493, in call INTERNALERROR> return self._hookexec(self.name, self._hookimpls, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_manager.py", line 115, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_callers.py", line 152, in _multicall INTERNALERROR> return outcome.get_result() INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_result.py", line 114, in get_result INTERNALERROR> raise exc.with_traceback(exc.traceback) INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_callers.py", line 77, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\filepath\new\Lib\site-packages_pytest\runner.py", line 114, in pytest_runtest_protocol
INTERNALERROR> runtestprotocol(item, nextitem=nextitem) INTERNALERROR> File "C:\file_path\new\Lib\site-packages_pytest\runner.py", line 127, in runtestprotocol INTERNALERROR> rep = call_and_report(item, "setup", log) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages_pytest\runner.py", line 226, in call_and_report INTERNALERROR> hook.pytest_runtest_logreport(report=report) INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_hooks.py", line 493, in call INTERNALERROR> return self._hookexec(self.name, self._hookimpls, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:file_path\new\Lib\site-packages\pluggy_manager.py", line 115, in _hookexec INTERNALERROR> return self._inner_hookexec(hook_name, methods, kwargs, firstresult) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pluggy_callers.py", line 113, in _multicall INTERNALERROR> raise exception.with_traceback(exception.traceback) INTERNALERROR> File "C:\filepath\new\Lib\site-packages\pluggy_callers.py", line 77, in _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> ^^^^^^^^^^^^^^^^^^^^^^^^^ INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pytest_html\basereport.py", line 238, in pytest_runtest_log report INTERNALERROR> self._hydrate_data(data, cells) INTERNALERROR> File "C:\file_path\new\Lib\site-packages\pytest_html\basereport.py", line 155, in _hydrate_data
INTERNALERROR> if "sortable" in self._report.table_header[index]: INTERNALERROR> ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ INTERNALERROR> IndexError: list index out of range

ramyavi avatar Sep 06 '23 06:09 ramyavi

Apologies, no longer valid as the format I have provided is incorrect. Still working on it but its no longer an issue. Thank you

ramyavi avatar Sep 06 '23 07:09 ramyavi

Reopening as this is still happening

ramyavi avatar Sep 06 '23 09:09 ramyavi

Can you show the updated code please?

Also, please share a dump of pip freeze and what OS and python version you're on.

Thanks.

BeyondEvil avatar Sep 09 '23 08:09 BeyondEvil

old code:


def pytest_html_results_table_header(cells):
    """
    Pytest hook responsible to maintain the table header in pytest html report
    """
    cells.insert(2, html.th("Source Data"))
    cells.insert(3, html.th("Expected Values"))
    cells.insert(4, html.th("Comparison Results"))


def pytest_html_results_table_row(report, cells):
    """
    Pytest hook responsible to maintain the table row in pytest html report
    """
    try:
        cells.insert(2, html.td(report.sourcedata))
        cells.insert(3, html.td(report.expected_values))
        if report.passed:
            cells.insert(4, html.td('Pass'))
        else:
            # cells.insert(4,html.td('Fail'))
            cells.insert(4, html.td(report.errormessage))
    except AttributeError:
        print("unable to make report inside the hook wrapper as obj is null ")

New code: (after removing py)


def pytest_html_results_table_header(cells):
    """
    Pytest hook responsible to maintain the table header in pytest html report
    """
    cells.insert(2,"<th>Source Data</th>")
    cells.insert(3,"<th>Expected Values</th>")
    cells.insert(4,"<th>Comparison Results</th>")


def pytest_html_results_table_row(report, cells):
    """
    Pytest hook responsible to maintain the table row in pytest html report
    """
    try:
        cells.insert(2, f"<td>Source Data: {report.sourcedata}</td>")
        cells.insert(3, f"<td>Expected Data: {report.expected_values}</td>")
        if report.passed:
            cells.insert(4, "<td>Pass</td>")
        else:
            cells.insert(4, f"<td>Comparison results: {report.errormessage}</td>")
    except AttributeError:
        print("unable to make report inside the hook wrapper as obj is null ")

pip freeze: aiohttp==3.8.5 aiosignal==1.3.1
allure-pytest==2.13.2
allure-python-commons==2.13.2 async-timeout==4.0.3
attrs==23.1.0
bcrypt==4.0.1
beautifulsoup4==4.12.2
build==1.0.3
cachetools==5.3.1
certifi==2023.7.22
cffi==1.15.1
charset-normalizer==3.2.0
click==8.1.7
colorama==0.4.6
cryptography==41.0.3
db-dtypes==1.1.1 decorator==5.1.1 et-xmlfile==1.1.0 Faker==14.2.1 frozenlist==1.4.0 fsspec==2023.6.0 gcsfs==2023.6.0 google-api-core==2.11.1 google-auth==2.22.0 google-auth-oauthlib==1.0.0 google-cloud-bigquery==3.11.4 google-cloud-core==2.3.3 google-cloud-storage==2.10.0 google-crc32c==1.5.0 google-resumable-media==2.5.0 googleapis-common-protos==1.60.0 grpcio==1.57.0 grpcio-status==1.57.0 idna==3.4 importlib-metadata==6.8.0 iniconfig==2.0.0 jaraco.classes==3.3.0 jeepney==0.8.0 Jinja2==3.1.2 jproperties==2.1.1 keyring==24.2.0 MarkupSafe==2.1.3 more-itertools==10.1.0 multidict==6.0.4 names==0.3.0 numpy==1.25.2 oauthlib==3.2.2 openpyxl==3.1.2 packaging==23.1 pandas==1.5.0 paramiko==2.12.0 pip-tools==7.3.0 pluggy==1.3.0 proto-plus==1.22.3 protobuf==4.24.0 pyarrow==12.0.1 pyasn1==0.5.0 pyasn1-modules==0.3.0 pycparser==2.21 PyNaCl==1.5.0 pyproject_hooks==1.0.0 pytest==7.4.0 pytest-html==4.0.0 pytest-metadata==3.0.0 python-dateutil==2.8.2 python-dotenv==1.0.0 pytz==2023.3 pywin32-ctypes==0.2.2 PyYAML==6.0.1 requests==2.31.0 requests-oauthlib==1.3.1 rsa==4.9 SecretStorage==3.3.3 six==1.16.0 soupsieve==2.4.1 urllib3==1.26.16 yarl==1.9.2 zipp==3.16.2

Python version: 3.11.4 OS - Windows 10 (64 bit)

Thanks

ramyavi avatar Sep 12 '23 10:09 ramyavi

I can't seem to repro this using 3.11.4 on Mac (don't have access to Windows).

Can you create a minimum project that recreates the error?

BeyondEvil avatar Sep 12 '23 10:09 BeyondEvil

Hello, Thanks for looking into this. I tried setting up a small project to replicate this issue and it does look like the problem is only when I put the functions(pytest_html_results_table_header, pytest_html_results_table_row, pytest_runtest_makereport) in a different file other than the conftest.py. Having these functions in the conftest.py seems to be working fine for the customised fields

ramyavi avatar Sep 22 '23 11:09 ramyavi