开发者

Python using doctest on the mainline

Hello i was wondering if it is possible and if so how? to do doctests or something similar from the mainline, instead of testing a function as is described in the doctest docs i.e.

"""
>>> 
Hello World
"""

if __name__ == "__main__":
    print "Hello"
    import doctest
    doctest.testmod()

This is part of being able to test students scripts against a docstring, i found this snipet of code that allows me to input both as strongs

import doctest
from doctest import DocTestRunner, DocTestParser
enter code here
def run_doctest(code, test):
    import doctest
    from doctest import DocTestRunner, DocTestParser
    code = code + '\n__dtest=__parser.get_doctest(__test, globals(), "Crunchy Doctest", "crunchy", 0)\n__runner.run(__dtest)\n'
    runner = DocTestRunner()
    parser = DocTestParser()
    exec code in {'__runner':runner, '__parser':parser, '__test':test}

that does more or less but it see开发者_如何学Pythonms hardly ideal, an suggestions on either point


doctest is not limited to testing functions. For example, if dt.py is:

'''
  >>> foo
  23
'''

foo = 23

if __name__ == '__main__':
    import doctest
    doctest.testmod()

then, e.g.:

$ py26 dt.py -v
Trying:
    foo
Expecting:
    23
ok
1 items passed all tests:
   1 tests in __main__
1 tests in 1 items.
1 passed and 0 failed.
Test passed.

(works just as well without the -v, but then it wouldn't have much to show: just silence;-). Is this what you're looking for?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜