Skip to content

Commit 7a73f6f

Browse files
committed
fix: Collapsed should support All and none
1 parent 34ff60f commit 7a73f6f

File tree

6 files changed

+171
-31
lines changed

6 files changed

+171
-31
lines changed

docs/user_guide.rst

+8-5
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,18 @@ Auto Collapsing Table Rows
248248

249249
By default, all rows in the **Results** table will be expanded except those that have :code:`Passed`.
250250

251-
This behavior can be customized either with a query parameter: :code:`?collapsed=Passed,XFailed,Skipped`
252-
or by setting the :code:`render_collapsed` in a configuration file (pytest.ini, setup.cfg, etc).
251+
This behavior can be customized with a query parameter: :code:`?collapsed=Passed,XFailed,Skipped`.
252+
If you want all rows to be collapsed you can pass :code:`?collapsed=All`.
253+
By setting the query parameter to empty string :code:`?collapsed=""` **none** of the rows will be collapsed.
254+
255+
Note that the query parameter is case insensitive, so passing :code:`PASSED` and :code:`passed` has the same effect.
256+
257+
You can also set the collapsed behaviour by setting the :code:`render_collapsed` in a configuration file (pytest.ini, setup.cfg, etc).
253258

254259
.. code-block:: ini
255260
256261
[pytest]
257-
render_collapsed = True
258-
259-
**NOTE:** Setting :code:`render_collapsed` will, unlike the query parameter, affect all statuses.
262+
render_collapsed = FIX THIS
260263
261264
Controlling Test Result Visibility Via Query Params
262265
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

src/pytest_html/scripts/datamanager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { getCollapsedCategory } = require('./storage.js')
22

33
class DataManager {
44
setManager(data) {
5-
const collapsedCategories = [...getCollapsedCategory(), 'passed']
5+
const collapsedCategories = [...getCollapsedCategory()]
66
const dataBlob = { ...data, tests: Object.values(data.tests).flat().map((test, index) => ({
77
...test,
88
id: `test_${index}`,

src/pytest_html/scripts/main.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { dom, findAll } = require('./dom.js')
33
const { manager } = require('./datamanager.js')
44
const { doSort } = require('./sort.js')
55
const { doFilter } = require('./filter.js')
6-
const { getVisible } = require('./storage.js')
6+
const { getVisible, possibleResults } = require('./storage.js')
77

88
const removeChildren = (node) => {
99
while (node.firstChild) {
@@ -61,16 +61,6 @@ const renderContent = (tests) => {
6161
}
6262

6363
const renderDerived = (tests, collectedItems, isFinished) => {
64-
const possibleResults = [
65-
{ result: 'passed', label: 'Passed' },
66-
{ result: 'skipped', label: 'Skipped' },
67-
{ result: 'failed', label: 'Failed' },
68-
{ result: 'error', label: 'Errors' },
69-
{ result: 'xfailed', label: 'Unexpected failures' },
70-
{ result: 'xpassed', label: 'Unexpected passes' },
71-
{ result: 'rerun', label: 'Reruns' },
72-
]
73-
7464
const currentFilter = getVisible()
7565
possibleResults.forEach(({ result, label }) => {
7666
const count = tests.filter((test) => test.result.toLowerCase() === result).length

src/pytest_html/scripts/storage.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
const possibleFilters = ['passed', 'skipped', 'failed', 'error', 'xfailed', 'xpassed', 'rerun']
1+
const possibleResults = [
2+
{ result: 'passed', label: 'Passed' },
3+
{ result: 'skipped', label: 'Skipped' },
4+
{ result: 'failed', label: 'Failed' },
5+
{ result: 'error', label: 'Errors' },
6+
{ result: 'xfailed', label: 'Unexpected failures' },
7+
{ result: 'xpassed', label: 'Unexpected passes' },
8+
{ result: 'rerun', label: 'Reruns' },
9+
]
10+
const possibleFilters = possibleResults.map((item) => item.result)
211

312
const getVisible = () => {
413
const url = new URL(window.location.href)
@@ -50,15 +59,28 @@ const setSort = (type) => {
5059
}
5160

5261
const getCollapsedCategory = () => {
53-
let categotries
62+
let categories
5463
if (typeof window !== 'undefined') {
5564
const url = new URL(window.location.href)
5665
const collapsedItems = new URLSearchParams(url.search).get('collapsed')
57-
categotries = collapsedItems?.split(',') || []
66+
switch (true) {
67+
case collapsedItems === null:
68+
categories = ['passed'];
69+
break;
70+
case collapsedItems?.length === 0 || /^["']{2}$/.test(collapsedItems):
71+
categories = [];
72+
break;
73+
case /^all$/.test(collapsedItems):
74+
categories = [...possibleFilters];
75+
break;
76+
default:
77+
categories = collapsedItems?.split(',').map(item => item.toLowerCase()) || [];
78+
break;
79+
}
5880
} else {
59-
categotries = []
81+
categories = []
6082
}
61-
return categotries
83+
return categories
6284
}
6385

6486
const getSortDirection = () => JSON.parse(sessionStorage.getItem('sortAsc'))
@@ -75,4 +97,6 @@ module.exports = {
7597
setSort,
7698
setSortDirection,
7799
getCollapsedCategory,
100+
possibleFilters,
101+
possibleResults,
78102
}

testing/test_integration.py

+78-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import random
66
import re
7+
import urllib.parse
78
from base64 import b64encode
89
from pathlib import Path
910

@@ -26,9 +27,16 @@
2627
}
2728

2829

29-
def run(pytester, path="report.html", *args):
30+
def run(pytester, path="report.html", cmd_flags=None, query_params=None):
31+
if cmd_flags is None:
32+
cmd_flags = []
33+
34+
if query_params is None:
35+
query_params = {}
36+
query_params = urllib.parse.urlencode(query_params)
37+
3038
path = pytester.path.joinpath(path)
31-
pytester.runpytest("-s", "--html", path, *args)
39+
pytester.runpytest("--html", path, *cmd_flags)
3240

3341
chrome_options = webdriver.ChromeOptions()
3442
if os.environ.get("CI", False):
@@ -48,7 +56,7 @@ def run(pytester, path="report.html", *args):
4856
continue
4957
# End workaround
5058

51-
driver.get(f"file:///reports{path}")
59+
driver.get(f"file:///reports{path}?{query_params}")
5260
return BeautifulSoup(driver.page_source, "html.parser")
5361
finally:
5462
driver.quit()
@@ -91,6 +99,10 @@ def get_text(page, selector):
9199
return get_element(page, selector).string
92100

93101

102+
def is_collapsed(page, test_name):
103+
return get_element(page, f".summary tbody[id$='{test_name}'] .expander")
104+
105+
94106
def get_log(page, test_id=None):
95107
# TODO(jim) move to get_text (use .contents)
96108
if test_id:
@@ -267,7 +279,7 @@ def pytest_html_report_title(report):
267279

268280
def test_resources_inline_css(self, pytester):
269281
pytester.makepyfile("def test_pass(): pass")
270-
page = run(pytester, "report.html", "--self-contained-html")
282+
page = run(pytester, cmd_flags=["--self-contained-html"])
271283

272284
content = file_content()
273285

@@ -349,7 +361,7 @@ def pytest_runtest_makereport(item, call):
349361
)
350362

351363
pytester.makepyfile("def test_pass(): pass")
352-
page = run(pytester, "report.html", "--self-contained-html")
364+
page = run(pytester, cmd_flags=["--self-contained-html"])
353365

354366
element = page.select_one(".summary a[class='col-links__extra text']")
355367
assert_that(element.string).is_equal_to("Text")
@@ -374,7 +386,7 @@ def pytest_runtest_makereport(item, call):
374386
)
375387

376388
pytester.makepyfile("def test_pass(): pass")
377-
page = run(pytester, "report.html", "--self-contained-html")
389+
page = run(pytester, cmd_flags=["--self-contained-html"])
378390

379391
content_str = json.dumps(content)
380392
data = b64encode(content_str.encode("utf-8")).decode("ascii")
@@ -435,7 +447,7 @@ def pytest_runtest_makereport(item, call):
435447
"""
436448
)
437449
pytester.makepyfile("def test_pass(): pass")
438-
page = run(pytester, "report.html", "--self-contained-html")
450+
page = run(pytester, cmd_flags=["--self-contained-html"])
439451

440452
# element = page.select_one(".summary a[class='col-links__extra image']")
441453
src = f"data:{mime_type};base64,{data}"
@@ -463,7 +475,7 @@ def pytest_runtest_makereport(item, call):
463475
"""
464476
)
465477
pytester.makepyfile("def test_pass(): pass")
466-
page = run(pytester, "report.html", "--self-contained-html")
478+
page = run(pytester, cmd_flags=["--self-contained-html"])
467479

468480
# element = page.select_one(".summary a[class='col-links__extra video']")
469481
src = f"data:{mime_type};base64,{data}"
@@ -477,7 +489,7 @@ def pytest_runtest_makereport(item, call):
477489

478490
def test_xdist(self, pytester):
479491
pytester.makepyfile("def test_xdist(): pass")
480-
page = run(pytester, "report.html", "-n1")
492+
page = run(pytester, cmd_flags=["-n1"])
481493
assert_results(page, passed=1)
482494

483495
def test_results_table_hook_insert(self, pytester):
@@ -624,3 +636,60 @@ def test_no_log(self, test_file, pytester):
624636
assert_that(log).contains("No log output captured.")
625637
for when in ["setup", "test", "teardown"]:
626638
assert_that(log).does_not_match(self.LOG_LINE_REGEX.format(when))
639+
640+
641+
class TestCollapsedQueryParam:
642+
@pytest.fixture
643+
def test_file(self):
644+
return """
645+
import pytest
646+
@pytest.fixture
647+
def setup():
648+
error
649+
650+
def test_error(setup):
651+
assert True
652+
653+
def test_pass():
654+
assert True
655+
656+
def test_fail():
657+
assert False
658+
"""
659+
660+
def test_default(self, pytester, test_file):
661+
pytester.makepyfile(test_file)
662+
page = run(pytester)
663+
assert_results(page, passed=1, failed=1, error=1)
664+
665+
assert_that(is_collapsed(page, "test_pass")).is_true()
666+
assert_that(is_collapsed(page, "test_fail")).is_false()
667+
assert_that(is_collapsed(page, "test_error::setup")).is_false()
668+
669+
@pytest.mark.parametrize("param", ["failed,error", "FAILED,eRRoR"])
670+
def test_specified(self, pytester, test_file, param):
671+
pytester.makepyfile(test_file)
672+
page = run(pytester, query_params={"collapsed": param})
673+
assert_results(page, passed=1, failed=1, error=1)
674+
675+
assert_that(is_collapsed(page, "test_pass")).is_false()
676+
assert_that(is_collapsed(page, "test_fail")).is_true()
677+
assert_that(is_collapsed(page, "test_error::setup")).is_true()
678+
679+
def test_all(self, pytester, test_file):
680+
pytester.makepyfile(test_file)
681+
page = run(pytester, query_params={"collapsed": "all"})
682+
assert_results(page, passed=1, failed=1, error=1)
683+
684+
for test_name in ["test_pass", "test_fail", "test_error::setup"]:
685+
assert_that(is_collapsed(page, test_name)).is_true()
686+
687+
@pytest.mark.parametrize("param", ["", 'collapsed=""', "collapsed=''"])
688+
def test_falsy(self, pytester, test_file, param):
689+
pytester.makepyfile(test_file)
690+
page = run(pytester, query_params={"collapsed": param})
691+
assert_results(page, passed=1, failed=1, error=1)
692+
693+
assert_that(is_collapsed(page, "test_pass")).is_false()
694+
assert_that(is_collapsed(page, "test_fail")).is_false()
695+
assert_that(is_collapsed(page, "test_error::setup")).is_false()

testing/unittest.js

+54
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,57 @@ describe('utils tests', () => {
156156
})
157157
})
158158
})
159+
160+
describe('Storage tests', () => {
161+
describe('getCollapsedCategory', () => {
162+
let originalWindow
163+
const mockWindow = (queryParam) => {
164+
const mock = {
165+
location: {
166+
href: `https://example.com/page?${queryParam}`
167+
}
168+
}
169+
originalWindow = global.window
170+
global.window = mock
171+
}
172+
after(() => global.window = originalWindow)
173+
174+
it('collapses passed by default', () => {
175+
mockWindow('')
176+
const collapsedItems = storageModule.getCollapsedCategory()
177+
expect(collapsedItems).to.eql(['passed'])
178+
})
179+
180+
it('collapses specified outcomes', () => {
181+
mockWindow('collapsed=failed,error')
182+
const collapsedItems = storageModule.getCollapsedCategory()
183+
expect(collapsedItems).to.eql(['failed', 'error'])
184+
})
185+
186+
it('collapses all', () => {
187+
mockWindow('collapsed=all')
188+
const collapsedItems = storageModule.getCollapsedCategory()
189+
expect(collapsedItems).to.eql(storageModule.possibleFilters)
190+
})
191+
192+
it('handles case insensitive params', () => {
193+
mockWindow('collapsed=fAiLeD,ERROR,passed')
194+
const collapsedItems = storageModule.getCollapsedCategory()
195+
expect(collapsedItems).to.eql(['failed', 'error', 'passed'])
196+
})
197+
198+
const falsy = [
199+
{ param: 'collapsed' },
200+
{ param: 'collapsed=' },
201+
{ param: 'collapsed=""' },
202+
{ param: 'collapsed=\'\'' }
203+
]
204+
falsy.forEach(({param}) => {
205+
it(`collapses none with ${param}`, () => {
206+
mockWindow(param)
207+
const collapsedItems = storageModule.getCollapsedCategory()
208+
expect(collapsedItems).to.be.empty
209+
})
210+
})
211+
})
212+
})

0 commit comments

Comments
 (0)