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

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -