Sphinx Doctest: test the code without comparing the output.

Luca Cerone luca.cerone at gmail.com
Mon Sep 23 10:51:30 EDT 2013


I don't know why but it seems that google groups stripped the indentation from the code. I just wanted to ensure you that in the examples that I have run
the definition of myfunc contained correctly indented code!

On Monday, 23 September 2013 15:45:43 UTC+1, Luca Cerone  wrote:
> .. 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.
> 
> 
> 
> The method you proposed still is not applicable, because I have no way to use startswith() and endswith()...
> 
> 
> 
> The following code could do what I want if I could ignore the output...
> 
> 
> 
>    >>> def myfun(x,verbose):
> 
>    ...    print "random output"
> 
>    ...    return x
> 
>    >>> result = myfun(10) #should ignore the output here!
> 
>    >>> print result
> 
>    10
> 
> 
> 
> fails with this message:
> 
> 
> 
> File "tutorial.rst", line 11, in example_fake
> 
> Failed example:
> 
>     result = myfun(10)
> 
> Expected nothing
> 
> Got:
> 
>     random output
> 
> 
> 
> (line 11 contains: >>> result = myfun(10))
> 
> 
> 
> A SKIP directive is not feasible either:
> 
> 
> 
> .. doctest:: example_fake
> 
> 
> 
>    >>> def myfun(x):
> 
>    ...    print "random output"
> 
>    ...    return x
> 
>    >>> result = myfun(10) # doctest: +SKIP 
> 
>    >>> result
> 
>    10
> 
> 
> 
> fails with this error message:
> 
> File "tutorial.rst", line 12, in example_fake
> 
> Failed example:
> 
>     result
> 
> Exception raised:
> 
>     Traceback (most recent call last):
> 
>       File "/usr/lib/python2.7/doctest.py", line 1289, in __run
> 
>         compileflags, 1) in test.globs
> 
>       File "<doctest example_fake[2]>", line 1, in <module>
> 
>         result
> 
>     NameError: name 'result' is not defined
> 
> 
> 
> As you can see is not that I want something too weird, is just that sometimes you can't control what the function display and ignoring the output is a reasonable way to implement a doctest.
> 
> 
> 
> Hope these examples helped to understand better what my problem is.
> 
> 
> 
> Thanks all of you guys for the hints, suggestions and best practices :)




More information about the Python-list mailing list