Testing dictionary results with doctest

Joe Strout joe at strout.net
Fri Oct 31 10:43:56 EDT 2008


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?

Thanks,
- Joe




More information about the Python-list mailing list