"""
def __init__(self, verbose=False, parser=DocTestParser(),
- recurse=True, _namefilter=None):
+ recurse=True, _namefilter=None,
+ exclude_empty=False):
"""
Create a new doctest finder.
If the optional argument `recurse` is false, then `find` will
only examine the given object, and not any contained objects.
+
+ If the optional argument `exclude_empty` is true, then `find`
+ will exclude tests for objects with empty docstrings.
"""
self._parser = parser
self._verbose = verbose
self._recurse = recurse
+ self._exclude_empty = exclude_empty
# _namefilter is undocumented, and exists only for temporary backward-
# compatibility support of testmod's deprecated isprivate mess.
self._namefilter = _namefilter
else:
try:
if obj.__doc__ is None:
- return None
- docstring = str(obj.__doc__)
+ docstring = ''
+ else:
+ docstring = str(obj.__doc__)
except (TypeError, AttributeError):
- return None
-
- # Don't bother if the docstring is empty.
- if not docstring:
- return None
+ docstring = ''
# Find the docstring's location in the file.
lineno = self._find_lineno(obj, source_lines)
+ # Don't bother if the docstring is empty.
+ if self._exclude_empty and not docstring:
+ return None
+
# Return a DocTest for this object.
if module is None:
filename = None
>>> (e.source, e.want, e.lineno)
('print sample_func(22)\n', '44\n', 3)
-If an object has no docstring, then a test is not created for it:
+By default, tests are created for objects with no docstring:
>>> def no_docstring(v):
... pass
- >>> finder.find(no_docstring)
+ >>> finder.find(no_docstring) # doctest: +ELLIPSIS
+ [<DocTest no_docstring from ... (no examples)>]
+
+However, the optional argument `exclude_empty` to the DocTestFinder
+constructor can be used to exclude tests for objects with empty
+docstrings:
+
+ >>> def no_docstring(v):
+ ... pass
+ >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True)
+ >>> excl_empty_finder.find(no_docstring)
[]
If the function has a docstring with no examples, then a test with no
1 SampleClass
3 SampleClass.NestedClass
1 SampleClass.NestedClass.__init__
+ 0 SampleClass.NestedClass.get
+ 0 SampleClass.NestedClass.square
1 SampleClass.__init__
2 SampleClass.a_classmethod
1 SampleClass.a_property
1 some_module.SampleClass
3 some_module.SampleClass.NestedClass
1 some_module.SampleClass.NestedClass.__init__
+ 0 some_module.SampleClass.NestedClass.get
+ 0 some_module.SampleClass.NestedClass.square
1 some_module.SampleClass.__init__
2 some_module.SampleClass.a_classmethod
1 some_module.SampleClass.a_property
will only be generated for it once:
>>> from test import doctest_aliases
- >>> tests = finder.find(doctest_aliases)
+ >>> tests = excl_empty_finder.find(doctest_aliases)
>>> tests.sort()
>>> print len(tests)
2
1 SampleClass
3 SampleClass.NestedClass
1 SampleClass.NestedClass.__init__
+ 0 SampleClass.NestedClass.get
+ 0 SampleClass.NestedClass.square
1 SampleClass.__init__
1 SampleClass.double
1 SampleClass.get
... return base == 'SampleClass'
>>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass)
>>> len(tests)
- 9
+ 11
Turning off Recursion
~~~~~~~~~~~~~~~~~~~~~