Sphinx Doctest: test the code without comparing the output.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Sep 21 08:27:31 EDT 2013


On Sat, 21 Sep 2013 03:47:26 -0700, Luca Cerone wrote:

> Dear all,
> I am writing the documentation for a Python package using Sphinx.
> 
> I have a problem when using doctest blocks in the documentation: I
> couldn't manage to get doctest to run a command but completely ignoring
> the output.
> 
> For example, how can I get a doctest like the following to run
> correctly?
> 
> .. doctest:: example_1
> 
>    >>> import random
>    >>> x = random.uniform(0,100)
>    >>> print str(x)
>    #some directive here to completely ignore the output

The Fine Manual says that the directive you want is #doctest:+SKIP.

http://docs.python.org/2/library/doctest.html#doctest.SKIP

Although it's not explicitly listed as a directive, every option flag 
corresponds to a directive. So your example becomes:

    >>> import random
    >>> x = random.uniform(0,100)
    >>> print x  #doctest:+SKIP
    42.012345678901234


(There's no need to convert things to str before printing them.)



-- 
Steven



More information about the Python-list mailing list