Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Testing dictionary results with doctest

Reply
Thread Tools

Testing dictionary results with doctest

 
 
Joe Strout
Guest
Posts: n/a
 
      10-31-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

 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      10-31-2008
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:
>>> r = foo()
>>> r == {'object': 'rain', 'location': 'Spain', 'subloc': 'train'}

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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      10-31-2008
On Oct 31, 2:43*pm, Joe Strout <(E-Mail Removed)> 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')]
"""
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      10-31-2008
Joe Strout <(E-Mail Removed)> writes:
> >>> t.match( "The rain in Spain falls mainly on the train." )
> {'object': 'rain', 'location': 'Spain', 'subloc': 'train'}


You can compare dictionaries for equality:

>>> t.match( "The rain in Spain falls mainly on the train." ) == \

{'object': 'rain', 'location': 'Spain', 'subloc': 'train'}
True
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
Store doctest verbose results to a variable mitchell Python 1 05-26-2005 05:46 PM



Advertisments