Testing dictionary results with doctest

MRAB google at mrabarnett.plus.com
Fri Oct 31 11:30:34 EDT 2008


On Oct 31, 2:43 pm, Joe Strout <j... at strout.net> wrote:
> I love doctest -- the way it combines documentation with verification  
> seems elegant and useful, and most of the time it's simple and easy to  
> use.
>
> But I've run into a bit of a snag trying to test a method that returns  
> a dictionary, because (of course) the order in which the dictionary  
> pairs are printed may not match what I wrote as the expected result.  
> For example, my doctest string is:
>
>         """
>         >>> t = Template("The $object in $location falls mainly on the  
> $subloc.")
>         >>> t.match( "The rain in Spain falls mainly on the train." )
>         {'object': 'rain', 'location': 'Spain', 'subloc': 'train'}
>         """
>
> But when I run it, I get:
>
> > Failed example:
> >     t.match( "The rain in Spain falls mainly on the train." )
> > Expected:
> >     {'object': 'rain', 'location': 'Spain', 'subloc': 'train'}
> > Got:
> >     {'subloc': 'train', 'object': 'rain', 'location': 'Spain'}
>
> Now, you and I can see that the obtained results really do match the  
> expected results, considered as a dictionary rather than as a string.  
> But doctest doesn't see it that way.
>
> What's the standard solution for this?  Should I iterate over the  
> sorted keys and print those out instead?  Is there some built-in  
> method somewhere that will print a dictionary in a reliable order?  
> Does doctest have some special way to tell it to consider the result  
> as a dictionary rather than a string?  Or something else?
>
How about:

"""
>>> t = Template("The $object in $location falls mainly on the $subloc.")
>>> sorted(t.match( "The rain in Spain falls mainly on the train." ).items())
[('location', 'Spain'), ('object', 'rain'), ('subloc', 'train')]
"""



More information about the Python-list mailing list