[Grok-dev] need advice on testing

Peter Bengtsson peter at fry-it.com
Mon May 26 06:41:01 EDT 2008


[snip]
>
>>>> pprint(create_person({
>>>>     lastname='Smith',
>>>>     firstname='Edward',
>>>>     ssn=19876223,
>>>>     role='Employee',
>>>>     title='Research Assistant'}))
> {'accounts': [{'gid': 482,
>               'uid': 1072,
>               'username': 'esmith7'}],
>  'firstname': 'Edward',
>  'guests': [],
>  'lastname': 'Smith',
>  'lastupdate': datetime.datetime(2008, ...)
>  'role': 'Employee',
>  'ssn': 19876223,
>  'title': 'Research Assistant'}
>
> My voyage so far has gone something like this:
>
>  - Doing this with a normal doctest is a nightmare, since the dictionary
>   can print out in any order.
>

I've solved a similar problem like that before by using sorted()

>>> d = dict(name='peter', age=28, name_of_something='Foo', this='is really long')
>>> from pprint import pprint
>>> pprint(sorted(d.items()))
[('age', 28),
 ('name', 'peter'),
 ('name_of_something', 'Foo'),
 ('this', 'is really long')]

That way the order is guaranteed.

-- 
Peter Bengtsson,
work www.fry-it.com
home www.peterbe.com
hobby www.issuetrackerproduct.com


More information about the Grok-dev mailing list