How do I test dictionary-equality with Python's doctest-package? -
i'm writing doctest function outputs dictionary. doctest looks like
>>> my_function() {'this': 'is', 'a': 'dictionary'}
when run it, fails with
expected: {'this': 'is', 'a': 'dictionary'} got: {'a': 'dictionary', 'this': 'is'}
my best guess cause of failure doctest isn't checking dictionary equality, __repr__
equality. this post indicates there's way trick doctest checking dictionary equality. how can this?
doctest doesn't check __repr__
equality, per se, checks output same. have ensure whatever printed same same dictionary. can one-liner:
>>> sorted(my_function().items()) [('a', 'dictionary'), ('this', 'is')]
although variation on solution might cleaner:
>>> my_function() == {'this': 'is', 'a': 'dictionary'} true
Comments
Post a Comment