Testing dictionary results with doctest

J

Joe Strout

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
 
B

bearophileHUGS

Joe Strout:
What's the standard solution for this?

I don't know of any standard solution, I generally sort the items in
some ways, or add the result, that has to be equal:
True


Does doctest have some special way to tell it to consider the result  
as a dictionary rather than a string?  Or something else?

A more general solution like this for sets/dics may become useful...
do you have any idea regarding how to design it?

Bye,
bearophile
 
M

MRAB

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:


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:

"""[('location', 'Spain'), ('object', 'rain'), ('subloc', 'train')]
"""
 
P

Paul Rubin

Joe Strout said:
>>> t.match( "The rain in Spain falls mainly on the train." )
{'object': 'rain', 'location': 'Spain', 'subloc': 'train'}

You can compare dictionaries for equality:
{'object': 'rain', 'location': 'Spain', 'subloc': 'train'}
True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top