161
161
162
162
163
163
def loadDocstringsFromModule (module ):
164
+ """
165
+ Load and return a test suite containing doctests from the specified module.
166
+
167
+ This function attempts to import the `doctest` module and uses it to find
168
+ and load all doctests defined in the provided module. If the module is
169
+ valid and contains doctests, a `unittest.TestSuite` object is returned
170
+ containing those tests. If no doctests are found or if an error occurs
171
+ during the loading process, appropriate messages are printed to the
172
+ console.
173
+
174
+ Notes:
175
+ - The function checks if the `doctest` module is already imported to
176
+ avoid unnecessary imports.
177
+ - The `DocTestFinder` is configured with the following options:
178
+ - `verbose=True`: Enables verbose output for the test discovery.
179
+ - `recurse=True`: Allows the finder to search for doctests in
180
+ nested functions and classes.
181
+ - `exclude_empty=True`: Excludes empty doctests from the results.
182
+ - If no doctests are found in the specified module, a message is printed
183
+ indicating that no tests were found.
184
+ - Any other exceptions encountered during the loading process are caught
185
+ and printed to the console.
186
+
187
+ Args:
188
+ module (module) -- The Python module from which to load doctests. This should be a
189
+ valid module object that has been imported. If the module is None,
190
+ the function will return None.
191
+
192
+ Returns:
193
+ (unittest.TestSuite or None) -- A `unittest.TestSuite` object containing the
194
+ doctests found in the specified module. If the module is None,
195
+ the function returns None.
196
+
197
+ Raises:
198
+ ImportError
199
+ If the `doctest` module fails to import, an ImportError is raised
200
+ with a message indicating the failure.
201
+
202
+ Meta-Testing:
203
+
204
+ >>> import multicast
205
+ >>> suite = loadDocstringsFromModule(multicast) #doctest: +ELLIPSIS
206
+ Finding tests in multicast...
207
+ >>> if suite:
208
+ ... print(f"Loaded {len(suite._tests)} doctests from "
209
+ ... f"{multicast.__name__}") # doctest: +ELLIPSIS
210
+ Loaded ... doctests from ...
211
+ >>>
212
+
213
+ """
164
214
if not module :
165
215
return None
166
216
try :
@@ -172,7 +222,13 @@ def loadDocstringsFromModule(module):
172
222
raise ImportError ("[CWE-440] doctest Failed to import." ) from _cause
173
223
finder = doctest .DocTestFinder (verbose = True , recurse = True , exclude_empty = True )
174
224
doc_suite = unittest .TestSuite ()
175
- doc_suite .addTests (doctest .DocTestSuite (module = module , test_finder = finder ))
225
+ try :
226
+ doc_suite .addTests (doctest .DocTestSuite (module = module , test_finder = finder ))
227
+ except ValueError as e :
228
+ # ValueError is raised when no tests are found
229
+ print (f"No doctests found in { module .__name__ } : { e } " )
230
+ except Exception as e :
231
+ print (f"Error loading doctests from { module .__name__ } : { e } " )
176
232
return doc_suite
177
233
178
234
0 commit comments