Skip to content

Commit

Permalink
Fix/improve comparison of byte strings
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored and nicoddemus committed Jun 25, 2019
1 parent 64a6365 commit 8c7eb82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/5260.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve/fix comparison of byte strings with Python 3.
5 changes: 4 additions & 1 deletion src/_pytest/assertion/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,11 @@ def _compare_eq_sequence(left, right, verbose=0):
"At index {} diff: {!r} != {!r}".format(i, left[i], right[i])
]
break
len_diff = len_left - len_right

if isinstance(left, bytes):
return explanation

len_diff = len_left - len_right
if len_diff:
if len_diff > 0:
dir_with_more = "Left"
Expand Down
12 changes: 12 additions & 0 deletions testing/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ def test_multiline_text_diff(self):
assert "- spam" in diff
assert "+ eggs" in diff

def test_bytes_diff(self):
diff = callequal(b"spam", b"eggs")
if PY3:
assert diff == [
"b'spam' == b'eggs'",
"At index 0 diff: 115 != 101",
"Use -v to get the full diff",
]
else:
# Handled as text on Python 2.
assert diff == ["'spam' == 'eggs'", "- spam", "+ eggs"]

def test_list(self):
expl = callequal([0, 1], [0, 2])
assert len(expl) > 1
Expand Down

0 comments on commit 8c7eb82

Please sign in to comment.