From e6b8b5da033be46ed1c6bd48996afd1355736338 Mon Sep 17 00:00:00 2001 From: cidlik Date: Thu, 9 May 2024 18:10:12 +0300 Subject: [PATCH 1/3] Add expand_logs ini option Is some cases it is more convenient to expand all logs by default, e.g., if you have several tests with long logs and you need to find something. --- src/pytest_html/plugin.py | 6 ++++++ src/pytest_html/report_data.py | 3 +++ src/pytest_html/scripts/datamanager.js | 4 ++++ src/pytest_html/scripts/main.js | 6 ++++++ src/pytest_html/scripts/storage.js | 11 +++++++++++ 5 files changed, 30 insertions(+) diff --git a/src/pytest_html/plugin.py b/src/pytest_html/plugin.py index 949a6ffa..f6df306f 100644 --- a/src/pytest_html/plugin.py +++ b/src/pytest_html/plugin.py @@ -72,6 +72,12 @@ def pytest_addoption(parser): default="result", help="column to initially sort on.", ) + parser.addini( + "expand_logs", + type="string", + default=False, + help="expand all logs by default.", + ) parser.addini( "generate_report_on_test", type="bool", diff --git a/src/pytest_html/report_data.py b/src/pytest_html/report_data.py index fd002099..9fc99941 100644 --- a/src/pytest_html/report_data.py +++ b/src/pytest_html/report_data.py @@ -61,6 +61,9 @@ def __init__(self, config): initial_sort = config.getini("initial_sort") self._data["initialSort"] = initial_sort + expand_logs = config.getini("expand_logs") + self._data["expandLogs"] = expand_logs + @property def additional_summary(self): return self._additional_summary diff --git a/src/pytest_html/scripts/datamanager.js b/src/pytest_html/scripts/datamanager.js index b95e95d9..3845d722 100644 --- a/src/pytest_html/scripts/datamanager.js +++ b/src/pytest_html/scripts/datamanager.js @@ -57,6 +57,10 @@ class DataManager { get initialSort() { return this.data.initialSort } + + get expandLogs() { + return this.data.expandLogs + } } module.exports = { diff --git a/src/pytest_html/scripts/main.js b/src/pytest_html/scripts/main.js index f01f2eac..5827bbba 100644 --- a/src/pytest_html/scripts/main.js +++ b/src/pytest_html/scripts/main.js @@ -6,6 +6,7 @@ const { getVisible, getCollapsedIds, setCollapsedIds, + getExpandLogs, getSort, getSortDirection, possibleFilters, @@ -47,10 +48,12 @@ const addItemToggleListener = (elem) => { const renderContent = (tests) => { const sortAttr = getSort(manager.initialSort) + const expandLogs = getExpandLogs(manager.expandLogs) const sortAsc = JSON.parse(getSortDirection()) const rows = tests.map(dom.getResultTBody) const table = document.getElementById('results-table') const tableHeader = document.getElementById('results-table-head') + const clickEvent = new Event('click'); const newTable = document.createElement('table') newTable.id = 'results-table' @@ -70,6 +73,9 @@ const renderContent = (tests) => { find('.logexpander', row).addEventListener('click', (evt) => evt.target.parentNode.classList.toggle('expanded'), ) + if (expandLogs) { + find('.logexpander', row).dispatchEvent(clickEvent) + } newTable.appendChild(row) } }) diff --git a/src/pytest_html/scripts/storage.js b/src/pytest_html/scripts/storage.js index ac39c12b..f4fce5b3 100644 --- a/src/pytest_html/scripts/storage.js +++ b/src/pytest_html/scripts/storage.js @@ -47,6 +47,16 @@ const showCategory = (categoryToShow) => { window.history.pushState({}, null, unescape(url.href)) } +const getExpandLogs = (expandLogs) => { + if (expandLogs === "true") { + return true + } + if (expandLogs) { + return true + } + return false +} + const getSort = (initialSort) => { const url = new URL(window.location.href) let sort = new URLSearchParams(url.search).get('sort') @@ -99,6 +109,7 @@ module.exports = { showCategory, getCollapsedIds, setCollapsedIds, + getExpandLogs, getSort, setSort, getSortDirection, From cee64489d42db5addc9cf0545c01e4178003614d Mon Sep 17 00:00:00 2001 From: cidlik Date: Thu, 9 May 2024 18:30:35 +0300 Subject: [PATCH 2/3] Fix errors founded by eslint --- src/pytest_html/scripts/main.js | 2 +- src/pytest_html/scripts/storage.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pytest_html/scripts/main.js b/src/pytest_html/scripts/main.js index 5827bbba..d628fb30 100644 --- a/src/pytest_html/scripts/main.js +++ b/src/pytest_html/scripts/main.js @@ -53,7 +53,7 @@ const renderContent = (tests) => { const rows = tests.map(dom.getResultTBody) const table = document.getElementById('results-table') const tableHeader = document.getElementById('results-table-head') - const clickEvent = new Event('click'); + const clickEvent = new Event('click') const newTable = document.createElement('table') newTable.id = 'results-table' diff --git a/src/pytest_html/scripts/storage.js b/src/pytest_html/scripts/storage.js index f4fce5b3..f217f69a 100644 --- a/src/pytest_html/scripts/storage.js +++ b/src/pytest_html/scripts/storage.js @@ -48,7 +48,7 @@ const showCategory = (categoryToShow) => { } const getExpandLogs = (expandLogs) => { - if (expandLogs === "true") { + if (expandLogs === 'true') { return true } if (expandLogs) { From e76199b716a6848f8dbba3575da063572719faa2 Mon Sep 17 00:00:00 2001 From: cidlik <81871604+cidlik@users.noreply.github.com> Date: Thu, 9 May 2024 19:26:00 +0300 Subject: [PATCH 3/3] Describe expand_logs in docs --- docs/user_guide.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/user_guide.rst b/docs/user_guide.rst index 691c86f2..5cf5252e 100644 --- a/docs/user_guide.rst +++ b/docs/user_guide.rst @@ -276,6 +276,16 @@ Note that the query parameter takes precedence. [pytest] render_collapsed = failed,error +Expand all logs +~~~~~~~~~~~~~~~ + +By default, logs are shown in short form. To display them in full form use ini option `expand_logs`. + +.. code-block:: ini + + [pytest] + expand_logs = true + Controlling Test Result Visibility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~