Testing dictionary results with doctest

Discussion in 'Python' started by Joe Strout, Oct 31, 2008.

  1. Joe Strout

    Joe Strout Guest

    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
     
    Joe Strout, Oct 31, 2008
    #1
    1. Advertising

  2. Joe Strout

    Guest

    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
     
    , Oct 31, 2008
    #2
    1. Advertising

  3. Joe Strout

    MRAB Guest

    On Oct 31, 2:43 pm, Joe Strout <> 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')]
    """
     
    MRAB, Oct 31, 2008
    #3
  4. Joe Strout

    Paul Rubin Guest

    Joe Strout <> 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
     
    Paul Rubin, Oct 31, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mitchell
    Replies:
    1
    Views:
    266
    Fredrik Lundh
    May 26, 2005
  2. Ilias Lazaridis
    Replies:
    6
    Views:
    443
    Ilias Lazaridis
    Feb 21, 2006
  3. james_027
    Replies:
    1
    Views:
    328
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  4. Navkirat Singh
    Replies:
    6
    Views:
    3,066
    Navkirat Singh
    Jul 29, 2010
  5. Chris Rebert
    Replies:
    0
    Views:
    529
    Chris Rebert
    Jul 29, 2010
Loading...

Share This Page