From 4ee12c42ccdf0274bed2a6fac14811658c7f9d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Br=C3=A4nnlund?= Date: Tue, 7 Nov 2023 15:19:54 +0100 Subject: [PATCH] fix: original sort order (#768) --- docs/changelog.rst | 7 +++++ src/pytest_html/scripts/main.js | 2 +- testing/test_integration.py | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 01ee247b..a798d83b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,13 @@ Versions follow `Semantic Versioning`_ (``..``). Version History --------------- +Unreleased +~~~~~~~~~~ + +* Fix original initial sort INI-setting. + + * Thanks to `@sturmf `_ for reporting. + 4.1.0 (2023-11-04) ~~~~~~~~~~~~~~~~~~ diff --git a/src/pytest_html/scripts/main.js b/src/pytest_html/scripts/main.js index 4f2c5adb..f01f2eac 100644 --- a/src/pytest_html/scripts/main.js +++ b/src/pytest_html/scripts/main.js @@ -57,7 +57,7 @@ const renderContent = (tests) => { // remove all sorting classes and set the relevant findAll('.sortable', tableHeader).forEach((elem) => elem.classList.remove('asc', 'desc')) - tableHeader.querySelector(`.sortable[data-column-type="${sortAttr}"]`).classList.add(sortAsc ? 'desc' : 'asc') + tableHeader.querySelector(`.sortable[data-column-type="${sortAttr}"]`)?.classList.add(sortAsc ? 'desc' : 'asc') newTable.appendChild(tableHeader) if (!rows.length) { diff --git a/testing/test_integration.py b/testing/test_integration.py index 50fecc70..145dd34b 100644 --- a/testing/test_integration.py +++ b/testing/test_integration.py @@ -823,6 +823,52 @@ def test_log_escaping(self, pytester, outcome, occurrence): count = log.count(each) assert_that(count).is_equal_to(occurrence) + @pytest.mark.parametrize( + "sort, order", + [ + (None, ["BBB", "AAA", "CCC"]), + ("result", ["BBB", "AAA", "CCC"]), + ("testId", ["AAA", "BBB", "CCC"]), + ("duration", ["CCC", "BBB", "AAA"]), + ("original", ["AAA", "BBB", "CCC"]), + ], + ) + def test_initial_sort(self, pytester, sort, order): + if sort is not None: + pytester.makeini( + f""" + [pytest] + initial_sort = {sort} + """ + ) + + pytester.makepyfile( + """ + import pytest + from time import sleep + + def test_AAA(): + sleep(0.3) + assert True + + def test_BBB(): + sleep(0.2) + assert False + + def test_CCC(): + sleep(0.1) + assert True + """ + ) + + page = run(pytester) + assert_results(page, passed=2, failed=1) + + result = page.select("td.col-testId") + assert_that(result).is_length(3) + for row, expected in zip(result, order): + assert_that(row.string).contains(expected) + class TestLogCapturing: LOG_LINE_REGEX = r"\s+this is {}"