Sphinx Doctest: test the code without comparing the output.

Neil Cerutti neilc at norwich.edu
Mon Sep 23 12:44:22 EDT 2013


On 2013-09-23, Luca Cerone <luca.cerone at gmail.com> wrote:
>> It won't be very good documenation any more but nothing stops you
>> 
>> from examining the result in the next doctest and making yourself
>> 
>> happy about it.
>> 
>> 
>> 
>>   >>> x = input("indeterminate:")
>> 
>>   >>> result = "'{}'".format(x))
>> 
>>   >>> result.startswith("'") and result.endswith("'")
>> 
>>   True
>> 
>
> Hi Neil, thanks for the hint, but this won't work.
>
> The problem is that the function displays some output informing
> you of what steps are being performed (some of which are
> displayed by a 3rd party function that I don't control).
>
> This output "interferes" with the output that should be checked by doctest.
>
> For example, you can check that the following doctest would fail:
>
> .. doctest:: example_fake
>
>    >>> def myfun(x,verbose):
>    ...    print "random output"
>    ...    return x
>    >>> myfun(10)
>    10
>
> When you run make doctest the test fails with this message:
>
> File "tutorial.rst", line 11, in example_fake
> Failed example:
>     myfun(10)
> Expected:
>     10
> Got:
>     random output
>     10
>
> In this case (imagine that "random output" is really random,
> therefore I can not easily filter it, if not ignoring several
> lines. This would be quite easy if ellipsis and line
> continuation wouldn't have the same sequence of characters, but
> unfortunately this is not the case.

Perhaps try the "advanced API" and define your oen OutputChecker
to add the feature that you need.

Figuring out how to best invoke doctest with your modified
OutputChecker will take some digging in the source, probably
looking at doctest.testmod. I don't see an example in the docs.

> Hope these examples helped to understand better what my problem
> is.

Yes, I think it's well-defined now.

-- 
Neil Cerutti



More information about the Python-list mailing list