Skip to content

Commit 9b7475a

Browse files
authored
Merge pull request #262 from ExaltedBagel/245-sort-dictionaries
Added support for ordering metadata dictionaries by key.
2 parents 2513821 + e63d5ca commit 9b7475a

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

pytest_html/plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,12 @@ def _generate_environment(self, config):
528528
if isinstance(value, str) and value.startswith("http"):
529529
value = html.a(value, href=value, target="_blank")
530530
elif isinstance(value, (list, tuple, set)):
531-
value = ", ".join(str(i) for i in value)
532-
rows.append(html.tr(html.td(key), html.td(value)))
531+
value = ", ".join(str(i) for i in sorted(map(str, value)))
532+
elif isinstance(value, dict):
533+
sorted_dict = {k: value[k] for k in sorted(value)}
534+
value = json.dumps(sorted_dict)
535+
raw_value_string = raw(str(value))
536+
rows.append(html.tr(html.td(key), html.td(raw_value_string)))
533537

534538
environment.append(html.table(rows, id="environment"))
535539
return environment

testing/test_pytest_html.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,22 @@ def pytest_configure(config):
664664
assert "Environment" in html
665665
assert len(re.findall(content, html)) == 1
666666

667-
def test_environment_list_value(self, testdir):
668-
content = tuple(str(random.random()) for i in range(10))
669-
content += tuple(random.random() for i in range(10))
670-
expected_content = ", ".join(str(i) for i in content)
667+
_unsorted_tuples = [
668+
("Hello", "fzWZP6vKRv", "hello", "garAge", "123Go"),
669+
(2, 4, 2, 1, 54),
670+
("Yes", 400, "5.4"),
671+
]
672+
_sorted_tuples = [
673+
"123Go, Hello, fzWZP6vKRv, garAge, hello",
674+
"1, 2, 2, 4, 54",
675+
"400, 5.4, Yes",
676+
]
677+
_test_environment_list_value_data_set = zip(_unsorted_tuples, _sorted_tuples)
678+
679+
@pytest.mark.parametrize(
680+
"content,expected_content", _test_environment_list_value_data_set
681+
)
682+
def test_environment_list_value(self, testdir, content, expected_content):
671683
expected_html_re = fr"<td>content</td>\n\s+<td>{expected_content}</td>"
672684
testdir.makeconftest(
673685
f"""
@@ -682,6 +694,46 @@ def pytest_configure(config):
682694
assert "Environment" in html
683695
assert len(re.findall(expected_html_re, html)) == 1
684696

697+
_unordered_dict = {k: len(k) for k in _unsorted_tuples[0]}
698+
_unordered_dict_expected = (
699+
r'<td>content</td>\n\s+<td>{"123Go": 5, "Hello": 5, '
700+
r'"fzWZP6vKRv": 10, "garAge": 6, "hello": 5}</td>'
701+
)
702+
_unordered_dict_with_html = {
703+
"First Link": r'<a href="https://www.w3schools.com">W3Schools</a>',
704+
"Second Link": r'<a href="https://www.w3schools.com">W2Schools</a>',
705+
"Third Link": r'<a href="https://www.w3schools.com">W4Schools</a>',
706+
}
707+
_unordered_dict_with_html_expected = (
708+
r"<td>content</td>\n\s+<td>{"
709+
r'"First Link": "<a href=\\"https://www.w3schools.com\\">W3Schools</a>", '
710+
r'"Second Link": "<a href=\\"https://www.w3schools.com\\">W2Schools</a>", '
711+
r'"Third Link": "<a href=\\"https://www.w3schools.com\\">W4Schools</a>"}</td>'
712+
)
713+
714+
@pytest.mark.parametrize(
715+
"unordered_dict,expected_output",
716+
[
717+
(_unordered_dict, _unordered_dict_expected),
718+
(_unordered_dict_with_html, _unordered_dict_with_html_expected),
719+
],
720+
)
721+
def test_environment_unordered_dict_value(
722+
self, testdir, unordered_dict, expected_output
723+
):
724+
testdir.makeconftest(
725+
f"""
726+
def pytest_configure(config):
727+
values = dict({json.dumps(unordered_dict)})
728+
config._metadata['content'] = values
729+
"""
730+
)
731+
testdir.makepyfile("def test_pass(): pass")
732+
result, html = run(testdir)
733+
assert result.ret == 0
734+
assert "Environment" in html
735+
assert len(re.findall(expected_output, html)) == 1
736+
685737
def test_environment_ordered(self, testdir):
686738
testdir.makeconftest(
687739
"""

0 commit comments

Comments
 (0)