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

Being able to choose the default initial-sort column #521

Closed
harmin-parra opened this issue Jul 1, 2022 · 11 comments
Closed

Being able to choose the default initial-sort column #521

harmin-parra opened this issue Jul 1, 2022 · 11 comments

Comments

@harmin-parra
Copy link
Contributor

harmin-parra commented Jul 1, 2022

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 ?

@BeyondEvil
Copy link
Contributor

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

@BeyondEvil
Copy link
Contributor

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

@harmin-parra
Copy link
Contributor Author

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"),
            ]

@BeyondEvil
Copy link
Contributor

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
Copy link
Contributor

Can you try with 4.0.0rc0?

@harmin-parra
Copy link
Contributor Author

harmin-parra commented May 8, 2023

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.

@BeyondEvil
Copy link
Contributor

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

@harmin-parra
Copy link
Contributor Author

harmin-parra commented May 9, 2023

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
Copy link
Contributor Author

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

@BeyondEvil
Copy link
Contributor

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.

@harmin-parra
Copy link
Contributor Author

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.",
    )

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

2 participants