[ACCEPTED]-Python doctest: Skip entire block?-doctest

Accepted answer
Score: 23

Wrap the example in a function and then 1 skip the function call:

"""
>>> def example():
...    from packagename import module
...    module.show_real_world_usage()
...
>>> example() # doctest: +SKIP
'Hello world!'
"""
Score: 7

My solution has been to trim the the 3-character 6 >>> and ... leaders where I want doctest to skip 5 over them, making them 2-characters.

So

"""
>>> from packagename import module
>>> module.show_real_world_usage()
'Hello world!'
"""

has 4 become

"""
>> from packagename import module
>> module.show_real_world_usage()
'Hello world!'
"""

Epydoc doesn't display this as nicely 3 as it does doctests, but I can live with 2 this. A skip-until-further-notice directive 1 in doctest would be welcome though.

Score: 3

with xdoctest you can use this >>> # doctest: +SKIP as first line:

def some_function():
    """
    Some documentation
    
    Examples:
        
        >>> # doctest: +SKIP
        >>> do_something() 
        >>> do_something_else()
        >>> and_do_this()
    """

unfortunately 2 doctest itself crashes (and sphinx prints that 1 as >>> instead of completely ignoring it).

Score: 2

If it's not an actual doctest by any means, you 7 can just assign the value to a variable. For 6 example,

example_usage = """
Real-world example:

# python2.5
...
"""

will cause that "test" to not be 5 evaluated.

It might be better to use __example_usage__ (or 4 something else surrounded by double-underscores) so 3 that it's clear that's a "magic" variable 2 and not a variable to be used within the 1 context of the script.

Score: 2

A small workaround building upon RobM's answer preserves 2 the display/formatting by starting the example 1 with a >>> like that:

""" 
>>>
>> from packagename import module 
>> module.show_real_world_usage() 
'Hello world!' 
"""
Score: 0

I went for the # doctest: +SKIP on every line

def doc_test_test():
    """Doc tests
    
    Examples:
        
        >>> do_something1() # doctest: +SKIP
        >>> do_something2() # doctest: +SKIP
        >>> do_something3() # doctest: +SKIP
    """
    pass

0

More Related questions