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

Being able to choose the default initial-sort column

Open harmin-parra opened this issue 3 years ago • 4 comments
trafficstars

By default, the initial-sort css class is set to the Result column <th class="sortable initial-sort result" col="result">Result</th>

Could we be able to change the initial sorted column ? In my personal case, I prefer to have the sort applied to the Test column (col="name")

I do know we can achieve this by using the pytest_html_results_table_header hook.

But I think it would be easier and move convenient to be able to do this via a command-line option

Maybe by adding another group.addoption or parser.addini ? which of these two is better ?

harmin-parra avatar Jul 01 '22 22:07 harmin-parra

You can provide your own CSS file(s). It’s additive, so you should be able to override only the bits you want.

BeyondEvil avatar Jul 01 '22 23:07 BeyondEvil

If that doesn’t work, we could support a query param that sets initial sort.

BeyondEvil avatar Jul 01 '22 23:07 BeyondEvil

You could add the following lines of code

file: plugin.py

    group.addoption(
        "--initial_sort",
        action="store",
        default="result",
        help="Open the report sorted by the given column. Accepted values: 'result' or 'name'",
    )

file: html_report.py

        column = self.config.getoption('--initial_sort')
        if column == "name":
            cells = [
                html.th("Result", class_="sortable result", col="result"),
                html.th("Test", class_="sortable initial-sort", col="name"),
                html.th("Duration", class_="sortable", col="duration"),
                html.th("Links", class_="sortable links", col="links"),
            ]
        else:
            cells = [
                html.th("Result", class_="sortable result initial-sort", col="result"),
                html.th("Test", class_="sortable", col="name"),
                html.th("Duration", class_="sortable", col="duration"),
                html.th("Links", class_="sortable links", col="links"),
            ]

harmin-parra avatar Jul 02 '22 00:07 harmin-parra

Thanks, but we're rebuilding the plugin from scratch, so that won't be possible. But it will likely be straightforward to do it via query-param.

Look out for v4.0.0-rc1 in the coming weeks.

BeyondEvil avatar Jul 02 '22 00:07 BeyondEvil

Can you try with 4.0.0rc0?

BeyondEvil avatar Mar 05 '23 16:03 BeyondEvil

By reading the documentation and the JavaScript files, I realized you added a GET query parameter to let users choose the column to apply the initial sort.

I would have preferred the initially proposed idea: adding an INI option via parser.addini.

harmin-parra avatar May 08 '23 16:05 harmin-parra

By reading the documentation and the JavaScript files, I realized you added a GET query parameter to let users choose the column to apply the initial sort.

I would have preferred the initially proposed idea: adding an INI option via parser.addini.

Given a good enough argument to why, I'm happy to accept a PR which adds that.

@harmin-parra

BeyondEvil avatar May 08 '23 16:05 BeyondEvil

The pytest.ini file already accepts the render_collapsed option and it would be very handy to add a initial_sort option to choose the initial sort column.

It is cleaner and better to set these kind of options via a INI configuration file, instead of typing a long URL with several query parameters.

harmin-parra avatar May 09 '23 08:05 harmin-parra

The self_contained could also be set via the INI configuration, but that would require the property to be set in the __init__ constructor instead of the _generate_report method of the BaseReport class

harmin-parra avatar May 09 '23 09:05 harmin-parra

The self_contained could also be set via the INI configuration, but that would require the property to be set in the __init__ constructor instead of the _generate_report method of the BaseReport class

You can set self_contained via addopts.

And you’re right, it would be more consistent to have initial_sort available as an INI too.

BeyondEvil avatar May 09 '23 10:05 BeyondEvil

I see the fix is already present in the source code of the master branch

    parser.addini(
        "initial_sort",
        type="string",
        default="result",
        help="column to initially sort on.",
    )

harmin-parra avatar Aug 19 '23 09:08 harmin-parra