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

前端 未结 7 1497
眼角桃花
眼角桃花 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条回答
  •  猫巷女王i
    2020-12-09 07:54

    I found it useful to use the deepdiff package in my doctests when testing arbitrarily nested data. For example:

    def something_complicated():
        """
        >>> from deepdiff import DeepDiff
        >>> DeepDiff(something_complicated(),
        ...          {'expected': {'output': ['a', 'b', 'c']}},
        ...          ignore_order=True)
        {}
        """
        items = ['a', 'b', 'c']
        random.shuffle(items)
        return {'expected': {'output': items}}
    

提交回复
热议问题