Skip to content

Instantly share code, notes, and snippets.

@akheron
Created February 15, 2011 11:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akheron/827408 to your computer and use it in GitHub Desktop.
Save akheron/827408 to your computer and use it in GitHub Desktop.
Unit test helper for diffing Python data structures
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