Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13503.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes dict output in assertion failures to preserve insertion order. (#13503)
2 changes: 1 addition & 1 deletion src/_pytest/_io/saferepr.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def safeformat(obj: object) -> str:
with a short exception info.
"""
try:
return pprint.pformat(obj)
return pprint.pformat(obj, sort_dicts=False)
except Exception as exc:
return _format_repr_exception(exc, obj)

Expand Down
12 changes: 12 additions & 0 deletions testing/io/test_saferepr.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,15 @@ def __repr__(self):
assert saferepr_unlimited(A()).startswith(
"<[ValueError(42) raised in repr()] A object at 0x"
)


def test_saferepr_dict_insertion_order():
from _pytest._io.saferepr import safeformat

d = {}
d["z"] = 1
d["a"] = 2
d["m"] = 3
output = safeformat(d)
# output should contain 'z', 'a', 'm' in this order (not 'a', 'm', 'z')
assert output.find("'z'") < output.find("'a'") < output.find("'m'")
Loading