I have a sample doctest like this one.
"""
This is the "iniFileGenerator" module.
>>> hintFile = "./tests/unit_test_files/hint.txt"
>>> f = iniFileGenerator(hintFile)
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
def __init__(self, hintFilePath):
self.hintFilePath = hintFilePath
def hello(self):
"""
>>> f.hello()
hello
"""
print "hello"
if __name__ == "__main__":
import doctest
doctest.testmod()
When I execute this code, I got this error.
Failed example:
f.hello()
Exception raised:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/doctest.py", line 1254, in __run
compileflags, 1) in test.globs
File "<doctest __main__.iniFileGenerator.hello[0]>", line 1, in <module>
f.hello()
NameError: name 'f' is not defined
This error is caused by accessing 'f' which was not accessible when testing hello()
method.
Is there any way to share the object which is created before? Without it, one need to create object all the time when it's necessary.
def hello(self):
"""
hintFile = "./tests/unit_test_files/hint.txt"
>>> f = iniFileGenerator(hintFile)
>>> f.hello()
hello
"""
print "hello"