If I have a module called thing.py and in there I have a Thing class, I was wondering how to run doctests that only belong to the class. The basic way to run all doctests in the module (and any classes in there) is:
"""thing module doctest
>>> 4 + 4
9
"""
def _test():
import doctest
doctest.testmod()
class Thing(object):
"""docstring for Thing
>>> 3 + 3
7
"""
def __init__(self, arg):
self.arg = arg
def printMsg(self):
return "Hello %s !" % self.arg
if __name__ == "__main__":
_test()
If I want to just run the doctest in the class though, I can make a new method in the module like this:
def _testThing():Then run this in the "if __name__" part:
import doctest
thingy = Thing("world")
doctest.run_docstring_examples(thingy, {})
if __name__ == "__main__":
_testThing()
The part "run_docstring_examples(thingy, {})" takes two required arguments - the object and the globals to use in the tests.
