manually build a unittest/doctest object.

Peter Otten __peter__ at web.de
Tue Dec 8 10:31:39 EST 2015


Vincent Davis wrote:

> On Tue, Dec 8, 2015 at 2:06 AM, Peter Otten <__peter__ at web.de> wrote:
> 
>> >>> import doctest
>> >>> example = doctest.Example(
>> ...     "print('hello world')\n",
>> ...     want="hello world\n")
>> >>> test = doctest.DocTest([example], {}, None, None, None, None)
>> >>> runner = doctest.DocTestRunner(verbose=True)
>> >>> runner.run(test)
>> Trying:
>>     print('hello world')
>> Expecting:
>>     hello world
>> ok
>> TestResults(failed=0, attempted=1)
>>
> 
> ​and now how to do a multi line statement​.

doctest doesn't do tests with multiple *statements.* A docstring like

"""
>>> x = 42
>>> print(x)
42
"""

is broken into two examples:

>> import doctest
>>> p = doctest.DocTestParser()
>>> doctest.Example.__repr__ = lambda self: "Example(source={0.source!r}, 
want={0.want!r})".format(self)
>>> p.get_examples("""
... >>> x = 42
... >>> print(x)
... 42
... """)
[Example(source='x = 42\n', want=''), Example(source='print(x)\n', 
want='42\n')]





More information about the Python-list mailing list