[TIP] doctest with nested dictionaries

Graham Carlyle graham.carlyle at maplecroft.net
Fri Jul 6 03:08:47 PDT 2007


On Thu, 2007-07-05 at 08:13 -0400, Benji York wrote:
> > and for other parts of the response that may be dynamic substitute
> > placeholders...
> 
> Instead of using str.replace(), I'd either use ellipsis or, even better, 
>   the regular expression normalizer so the dynamic parts of the test 
> output are normalized automatically (the renormalizer is included in 
> zope.testing, see 
> http://svn.zope.org/zope.testing/trunk/src/zope/testing/renormalizing.py?view=markup).

This is one aspect of doctest that puzzles me. 

I can see the utility of ellipsis for output where you don't care what
the exact value is, and this "renormalizing" for parts of the output
where your only bothered that it conforms to a certain format. 

But what about when you do want the output to have a specific value but
its generated in a way that you don't have complete control and so can't
have its value in a string. For example a database id.

Is doctest taking the view, like the mockobject people, that you should
design your software so that you have no implicit dependencies and so
can always inject/set these values explicitly.

If not then isn't an obvious further step to allow simple substitution
within the output using some sort of template syntax?

    >>> foo_format(obj)
    {'id': {{ obj.db_id }}, 'name': 'bob'}

Graham






More information about the testing-in-python mailing list