How do I test dictionary-equality with Python's doctest-package?

前端 未结 7 1494
眼角桃花
眼角桃花 2020-12-09 07:15

I\'m writing a doctest for a function that outputs a dictionary. The doctest looks like

>>> my_function()
{\'this\': \'is\', \'a\': \'dictionary\'}
         


        
7条回答
  •  无人及你
    2020-12-09 08:03

    Another good way is to use pprint (in the standard library).

    >>> import pprint
    >>> pprint.pprint({"second": 1, "first": 0})
    {'first': 0, 'second': 1}
    

    According to its source code, it's sorting dicts for you:

    http://hg.python.org/cpython/file/2.7/Lib/pprint.py#l158

    items = _sorted(object.items())
    

提交回复
热议问题