pytest-html
pytest-html copied to clipboard
Being able to choose the default initial-sort column
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 ?
You can provide your own CSS file(s). It’s additive, so you should be able to override only the bits you want.
If that doesn’t work, we could support a query param that sets initial sort.
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"),
]
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.
Can you try with 4.0.0rc0?
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.
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
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.
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
The
self_containedcould also be set via the INI configuration, but that would require the property to be set in the__init__constructor instead of the_generate_reportmethod of theBaseReportclass
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.
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.",
)