Skip to content

Commit d6bc3ab

Browse files
committed
Added an extra test to cover the case where a href would be contained in the dictionary.
Dictionary string generation now uses json.dumps instead of the handmade json.dumps.
1 parent 753ca94 commit d6bc3ab

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

pytest_html/plugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,8 @@ def _generate_environment(self, config):
530530
elif isinstance(value, (list, tuple, set)):
531531
value = ", ".join(str(i) for i in sorted(map(str, value)))
532532
elif isinstance(value, dict):
533-
key_value_list = [f"'{k}': {value[k]}" for k in sorted(value)]
534-
value = ", ".join(key_value_list)
535-
value = "{" + value + "}"
533+
sorted_dict = {k: value[k] for k in sorted(value)}
534+
value = json.dumps(sorted_dict)
536535
raw_value_string = raw(str(value))
537536
rows.append(html.tr(html.td(key), html.td(raw_value_string)))
538537

testing/test_pytest_html.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -695,26 +695,44 @@ def pytest_configure(config):
695695
assert len(re.findall(expected_html_re, html)) == 1, html
696696

697697
_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+
)
698713

699-
@pytest.mark.parametrize("unordered_dict", [_unordered_dict])
700-
def test_environment_unordered_dict_value(self, testdir, unordered_dict):
701-
expected_html_re = (
702-
r"<td>content</td>\n\s+<td>{'123Go': 5, 'Hello': 5, "
703-
r"'fzWZP6vKRv': 10, 'garAge': 6, 'hello': 5}</td>"
704-
)
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+
):
705724
testdir.makeconftest(
706-
"""
725+
f"""
707726
def pytest_configure(config):
708-
values = dict({'Hello': 5, 'fzWZP6vKRv': 10, 'garAge': 6, 'hello': 5,
709-
'123Go': 5})
727+
values = dict({json.dumps(unordered_dict)})
710728
config._metadata['content'] = values
711729
"""
712730
)
713731
testdir.makepyfile("def test_pass(): pass")
714732
result, html = run(testdir)
715733
assert result.ret == 0
716734
assert "Environment" in html
717-
assert len(re.findall(expected_html_re, html)) == 1, html
735+
assert len(re.findall(expected_output, html)) == 1, html
718736

719737
def test_environment_ordered(self, testdir):
720738
testdir.makeconftest(

0 commit comments

Comments
 (0)