Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100168

Re: manually build a unittest/doctest object.

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: manually build a unittest/doctest object.
Date 2015-12-08 16:31 +0100
Organization None
Message-ID <mailman.73.1449588710.12405.python-list@python.org> (permalink)
References <CALyJZZXeRaFxxdDK6aNSZC4xyqn082kZCGtgYBQxnBqKU-_WEg@mail.gmail.com> <n466ip$bg7$1@ger.gmane.org> <CALyJZZVTQ6cFr2hcy=OHnAmOVwoiZTKpFERQRQFVGK08eT4HKA@mail.gmail.com>

Show all headers | View raw


Vincent Davis wrote:

> On Tue, Dec 8, 2015 at 2:06 AM, Peter Otten <__peter__@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')]

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: manually build a unittest/doctest object. Peter Otten <__peter__@web.de> - 2015-12-08 16:31 +0100

csiph-web