Created
February 15, 2011 11:12
-
-
Save akheron/827408 to your computer and use it in GitHub Desktop.
Unit test helper for diffing Python data structures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def eq_diff(a, b): | |
import json, difflib | |
from datetime import datetime | |
if a == b: | |
return | |
class DatetimeEncoder(json.JSONEncoder): | |
def default(self, o): | |
if isinstance(o, datetime): | |
return o.isoformat() | |
else: | |
# Fall back to string | |
return str(o) | |
a_json = json.dumps(a, indent=4, sort_keys=True, cls=DatetimeEncoder) | |
b_json = json.dumps(b, indent=4, sort_keys=True, cls=DatetimeEncoder) | |
diff = ''.join(difflib.unified_diff( | |
a_json.splitlines(True), | |
b_json.splitlines(True), | |
fromfile='actual', | |
tofile='expected', | |
)) | |
raise AssertionError('\n' + diff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment