manually build a unittest/doctest object.

Vincent Davis vincent at vincentdavis.net
Tue Dec 8 09:31:19 EST 2015


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​.

>>> import doctest
>>> example =
doctest.Example("print('hello')\nprint('world')",want="hello\nworld")
>>> test = doctest.DocTest([example], {}, None, None, None, None)
>>> runner = doctest.DocTestRunner(verbose=True)
>>> runner.run(test)

Trying:
    print('hello')
    print('world')
Expecting:
    hello
    world
**********************************************************************
Line 1, in None
Failed example:
    print('hello')
    print('world')
Exception raised:
    Traceback (most recent call last):
      File "/Users/vincentdavis/anaconda/envs/py35/lib/python3.5/doctest.py",
line 1320, in __run
        compileflags, 1), test.globs)
      File "<doctest None[0]>", line 1
        print('hello')
                     ^
    SyntaxError: multiple statements found while compiling a single statement



Vincent Davis



More information about the Python-list mailing list