Skip to content

Commit 27b9051

Browse files
committed
Added support for ordering metadata dictionaries by key. This compare requires casting the keys to string, as
far as I know.
1 parent 23186cd commit 27b9051

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

pytest_html/plugin.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,12 @@ def _generate_environment(self, config):
505505
if isinstance(value, str) and value.startswith("http"):
506506
value = html.a(value, href=value, target="_blank")
507507
elif isinstance(value, (list, tuple, set)):
508-
value = ", ".join(str(i) for i in value)
509-
rows.append(html.tr(html.td(key), html.td(value)))
508+
value = ", ".join(str(i) for i in sorted(map(str, value)))
509+
elif isinstance(value, dict):
510+
key_value_list = [f"'{k}': {value[k]}" for k in sorted(value)]
511+
value = ", ".join(key_value_list)
512+
value = "{" + value + "}"
513+
rows.append(html.tr(html.td(key), html.td(raw(value))))
510514

511515
environment.append(html.table(rows, id="environment"))
512516
return environment

testing/test_pytest_html.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,10 +633,22 @@ def pytest_configure(config):
633633
assert "Environment" in html
634634
assert len(re.findall(content, html)) == 1
635635

636-
def test_environment_list_value(self, testdir):
637-
content = tuple(str(random.random()) for i in range(10))
638-
content += tuple(random.random() for i in range(10))
639-
expected_content = ", ".join(str(i) for i in content)
636+
_unsorted_tuples = [
637+
("Hello", "fzWZP6vKRv", "hello", "garAge", "123Go"),
638+
(2, 4, 2, 1, 54),
639+
("Yes", 400, "5.4"),
640+
]
641+
_sorted_tuples = [
642+
"123Go, Hello, fzWZP6vKRv, garAge, hello",
643+
"1, 2, 2, 4, 54",
644+
"400, 5.4, Yes",
645+
]
646+
_test_environment_list_value_data_set = zip(_unsorted_tuples, _sorted_tuples)
647+
648+
@pytest.mark.parametrize(
649+
"content,expected_content", _test_environment_list_value_data_set
650+
)
651+
def test_environment_list_value(self, testdir, content, expected_content):
640652
expected_html_re = fr"<td>content</td>\n\s+<td>{expected_content}</td>"
641653
testdir.makeconftest(
642654
f"""
@@ -649,7 +661,29 @@ def pytest_configure(config):
649661
result, html = run(testdir)
650662
assert result.ret == 0
651663
assert "Environment" in html
652-
assert len(re.findall(expected_html_re, html)) == 1
664+
assert len(re.findall(expected_html_re, html)) == 1, html
665+
666+
_unordered_dict = {k: len(k) for k in _unsorted_tuples[0]}
667+
668+
@pytest.mark.parametrize("unordered_dict", [_unordered_dict])
669+
def test_environment_unordered_dict_value(self, testdir, unordered_dict):
670+
expected_html_re = (
671+
r"<td>content</td>\n\s+<td>{'123Go': 5, 'Hello': 5, "
672+
r"'fzWZP6vKRv': 10, 'garAge': 6, 'hello': 5}</td>"
673+
)
674+
testdir.makeconftest(
675+
"""
676+
def pytest_configure(config):
677+
values = dict({'Hello': 5, 'fzWZP6vKRv': 10, 'garAge': 6, 'hello': 5,
678+
'123Go': 5})
679+
config._metadata['content'] = values
680+
"""
681+
)
682+
testdir.makepyfile("def test_pass(): pass")
683+
result, html = run(testdir)
684+
assert result.ret == 0
685+
assert "Environment" in html
686+
assert len(re.findall(expected_html_re, html)) == 1, html
653687

654688
def test_environment_ordered(self, testdir):
655689
testdir.makeconftest(

0 commit comments

Comments
 (0)