pytest-dev/pytest-html

Sorting by custom columns no longer works

headtr1ck opened this issue · 3 comments

With the update to 4.1.1 the sorting of our custom columns stopped working.

First of all, the "sorting" only happens when I click on the header twice (which is not a hard requirement by us).
Second, the sorting does not work at all.
Our values in one row are e.g. ["CKD ATBD 6.2", "L1 ATBD 5.1", "CKD ATBD 8.1", "CKD ATBD 4.7"], this is already the output of the sorting. Interestingly the descending sorting keeps this wrong ordering in reverse. We have also tried this with only the numerical values, same result.
Another column simply contains short words separated by spaces, and the ordering is also broken there.

The header is modified the following way

def pytest_html_results_table_header(cells):
    cells.insert(
        1, '<th class="sortable keydata" data-column-type="keydata">Key Data</th>'
    )
    cells.insert(1, '<th class="sortable ref" data-column-type="ref">Reference</th>')
    # remove links column
    cells.pop()

And the cells are added:

def pytest_html_results_table_row(report, cells):
    ref = "CKD ATBD 5.1"  # extracted from report in actual code
    key = "Fancy"  # extracted from report in actual code
    cells.insert(1, f"<td>{ref}</td>")
    cells.insert(1, f"<td>{key}</td>")
    # Remove links column
    cells.pop()

Here is our environment.


Python | 3.9.10
Packages | pytest: 7.4.3 pluggy: 1.0.0
Plugins | sugar: 0.9.7 html: 4.1.1 xdist: 3.5.0 metadata: 3.0.0 cov: 4.1.0 anyio: 3.7.0

Does it work with previous 4.x versions?

According to the documentation. The row td's need a class name that matches the data-column-type. So in your case, this should solved it:

def pytest_html_results_table_row(report, cells):
    ref = "CKD ATBD 5.1"  # extracted from report in actual code
    key = "Fancy"  # extracted from report in actual code
    cells.insert(1, f"<td class=\"col-ref\">{ref}</td>")
    cells.insert(1, f"<td class=\"col-keydata\">{key}</td>")
    # Remove links column
    cells.pop()

Thanks! That fixes it.
I thought that this was just a css class for styling.
Good to know that it needs to be exactly this format (col-{data-column-type}).