manually build a unittest/doctest object.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Dec 8 00:56:52 EST 2015


On Tuesday 08 December 2015 14:30, 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

Not easily. Effectively, you would have to re-invent the doctest module and 
re-engineer it to accept a completely different format.

But if you are willing to write your tests in doctest format, this might 
help you:



import doctest

def rundoctests(text, name='<text>', globs=None, verbose=None,
               report=True, optionflags=0, extraglobs=None,
               raise_on_error=False,
               quiet=False,):
    # Assemble the globals.
    if globs is None:
        globs = globals()
    globs = globs.copy()
    if extraglobs is not None:
        globs.update(extraglobs)
    if '__name__' not in globs:
        globs['__name__'] = '__main__'
    # Parse the text looking for doc tests.
    parser = doctest.DocTestParser()
    test = parser.get_doctest(text, globs, name, name, 0)
    # Run the tests.
    if raise_on_error:
        runner = doctest.DebugRunner(
                verbose=verbose, optionflags=optionflags)
    else:
        runner = doctest.DocTestRunner(
                verbose=verbose, optionflags=optionflags)
    if quiet:
        runner.run(test, out=lambda s: None)
    else:
        runner.run(test)
    if report:
        runner.summarize()
    # Return a (named, if possible) tuple (failed, attempted).
    a, b = runner.failures, runner.tries
    try:
        TestResults = doctest.TestResults
    except AttributeError:
        return (a, b)
    return TestResults(a, b)



Then call rundoctests(text) to run any doc tests in text. By default, if 
there are no errors, it prints nothing. If there are errors, it prints the 
failing tests. Either way, it returns a tuple

    (number of failures, number of tests run)

Examples in use:

py> good_code = """
... >>> import math
... >>> print "Hello World!"
... Hello World!
... >>> math.sqrt(100)
... 10.0
... 
... """
py> rundoctests(good_code)
TestResults(failed=0, attempted=3)



py> bad_code = """
... >>> print 10
... 11
... """
py> rundoctests(bad_code)
**********************************************************************
File "<text>", line 2, in <text>
Failed example:
    print 10
Expected:
    11
Got:
    10
**********************************************************************
1 items had failures:
   1 of   1 in <text>
***Test Failed*** 1 failures.
TestResults(failed=1, attempted=1)







More information about the Python-list mailing list