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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -