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


Groups > comp.lang.python > #100132 > unrolled thread

manually build a unittest/doctest object.

Started byVincent Davis <vincent@vincentdavis.net>
First post2015-12-07 20:30 -0700
Last post2015-12-08 16:56 +1100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  manually build a unittest/doctest object. Vincent Davis <vincent@vincentdavis.net> - 2015-12-07 20:30 -0700
    Re: manually build a unittest/doctest object. Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-08 16:56 +1100

#100132 — manually build a unittest/doctest object.

FromVincent Davis <vincent@vincentdavis.net>
Date2015-12-07 20:30 -0700
Subjectmanually build a unittest/doctest object.
Message-ID<mailman.47.1449545442.12405.python-list@python.org>
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)

Thanks
Vincent Davis

[toc] | [next] | [standalone]


#100137

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-12-08 16:56 +1100
Message-ID<56667126$0$1497$c3e8da3$5496439d@news.astraweb.com>
In reply to#100132
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)



[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web