Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest_html_results_table_row hook is called twice when running tests #786

Closed
gregtwice opened this issue Dec 19, 2023 · 1 comment
Closed

Comments

@gregtwice
Copy link

When running tests, i get the following error, same as in issue #782

INTERNALERROR>     if "sortable" in self._report.table_header[index]:
INTERNALERROR>                      ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
INTERNALERROR> IndexError: list index out of range

The sample code is take verbatim from the documentation

When investingating the error and adding logging to the hook, i can see that it is called twice during the same test.
When inspecting the values of cells and cells and self._report.table_header in basereport.py in _hydrate_data, I see that the length of cells is 8 when self._report.table_header is 6. as the code is iterating on cells, this triggers an index error.

Has this ever happened to anyone else ?

here is the sample code

def pytest_html_results_table_header(cells):
    cells.insert(2, "<th>Description</th>")
    cells.insert(1, '<th class="sortable time" data-column-type="time">Time</th>')


def pytest_html_results_table_row(report, cells):
    description = report.description.replace(" ", "&nbsp").replace("\n", "<br />")
    cells.insert(2, f"<td>{description}</td>")
    cells.insert(1, f'<td class="col-time">{datetime.utcnow()}</td>')
    print(cells)


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

Here is the result of the print(cells) statement

['<td class="col-result">Passed</td>', '<td class="col-testId">test_suite_setUp::setup</td>', '<td class="col-duration">0 ms</td>', '<td class="col-links"></td>', '<td>None</td>', '<td class="col-time">2023-12-19 17:19:07.301758</td>']
['<td class="col-result">Passed</td>', '<td class="col-testId">test_suite_setUp::setup</td>', '<td class="col-duration">0 ms</td>', '<td class="col-links"></td>', '<td>None</td>', '<td class="col-time">2023-12-19 17:19:07.301758</td>', '<td>None</td>', '<td class="col-time">2023-12-19 17:19:07.302751</td>']

printing the content of report.call when filtering on report.when == "call" also gives 'setup' twice and then 'call' twice

@gregtwice
Copy link
Author

I found the answer, it was a weird bug due to the pwd of the working directory and the windows directory names.

Pytest was registering two conftests.py that were located in the same directory, but under two different paths, One with the windows path and one with the pwd that used the path with a different name:

PLUGIN registered: <module 'conftest' from 'C:\\Tests\\conftest.py'>
PLUGIN registered: <module 'conftest' from 'C:\\tests\\conftest.py'>

changing the path to the default windows one fixed the issue.
Closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant