manually build a unittest/doctest object.

Peter Otten __peter__ at web.de
Tue Dec 8 04:06:32 EST 2015


Vincent Davis wrote:

> If I have a string that is python code, for example
> mycode = "print('hello world')"
> myresult = "hello world"
> How can a "manually" build a unittest (doctest) and test I get myresult
> 
> I have attempted to build a doctest but that is not working.
> e = doctest.Example(source="print('hello world')/n", want="hello world\n")
> t = doctest.DocTestRunner()
> t.run(e)

There seems to be one intermediate step missing:

example --> doctest --> runner

>>> 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)

But why would you want to do that?




More information about the Python-list mailing list