Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python doctest: Skip entire block?

Tags:

python

doctest

I've got a Python module with docstrings in class methods, and a real-world example in the module docstring. The distinction is that the method-docstrings have been carefully crafted to be utterly repeatable tests, while the real-world example is just a copy'n'paste of the history from a Linux shell - which happened to invoke the python interpreter.

E.g.

"""
Real-world example:

# python2.5
Python 2.5 (release25-maint, Jul 20 2008, 20:47:25)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from packagename import module
>>> module.show_real_world_usage()
'Hello world!'
"""

class SomeClass(object):
    def someMethod(self):
        """
        >>> 1 == 1
        True
        """

I want to run the doctest in SomeClass.someMethod, but not in the module's docstrings.

Doctest's +SKIP directive only works per line, which would mean adding 10s of lines to my real-world example. Ugly!

Is there a way to make doctest skip an entire block? A bit like <!-- ... --> in HTML?

like image 519
RobM Avatar asked Nov 27 '09 14:11

RobM


People also ask

What is the correct way to run all the doctest in Python?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.

Can we handle unpredictable output using doctest?

When the tests include values that are likely to change in unpredictable ways, and where the actual value is not important to the test results, you can use the ELLIPSIS option to tell doctest to ignore portions of the verification value.

How does doctest work Python?

The doctest module programmatically searches Python code for pieces of text within comments that look like interactive Python sessions. Then, the module executes those sessions to confirm that the code referenced by a doctest runs as expected.


1 Answers

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

"""
>>> def example():
...    from packagename import module
...    module.show_real_world_usage()
...
>>> example() # doctest: +SKIP
'Hello world!'
"""
like image 75
lambacck Avatar answered Oct 06 '22 17:10

lambacck