By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,571 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,571 IT Pros & Developers. It's quick & easy.

Doc tests in Python

P: n/a
Hello. Im new to using doctests in python. Could some1 tel me how to
use doctests if i have a constructor in my code?
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply

P: n/a
En Thu, 19 Jun 2008 02:46:15 -0300, J-Burns <ar**********@gmail.com>
escribió:
Hello. Im new to using doctests in python. Could some1 tel me how to
use doctests if i have a constructor in my code?
Just construct the desired objects inside the doctest. See the difflib
module for a couple examples.
If it's hard/undesirable/annoying to construct the objects for every test,
you may use the "globs" or "extraglobs" argument to doctest.testmod.
Something like this:

class SomeClass:
def __init__(self, a, lot, of arguments, are, required):
...

def some_method(self, arg):
"""Does this and that...

In the test below, obj refers to the
already created instance (below, in _test)
>>obj.some_method(self, 123)
True
>>obj.some_method(self, "hi")
False
"""
...

def _test():
import doctest
obj = SomeClass(create, the instance, using, a, lot, of, arguments)
doctest.testmod(extraglobs={'obj': obj})

if __name__=='__main__':
_test()

But I prefer to keep the doctests self-contained whenever possible.

--
Gabriel Genellina

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.