Commits

Anonymous committed 39abe90

Added doctests plugin similar to nose's (loads doctests from modules and text files)

  • Participants
  • Parent commits 4ade25b

Comments (0)

Files changed (4)

nose2/plugins/doctests.py

+import doctest
+import os
+
+from unittest2 import loader, Plugin
+
+
+class DocTestLoader(Plugin):
+    configSection = 'doctest'
+    commandLineSwitch = (None, 'with-doctest',
+                         'Look for doctests in all modules')
+
+    def __init__(self):
+        self.extensions = self.config.as_list('extensions', ['.txt', '.rst'])
+
+    def handleFile(self, event):
+        loader_ = event.loader
+        path = event.path
+        _root, ext = os.path.splitext(path)
+        if ext in self.extensions:
+            suite = doctest.DocFileTest(path, module_relative=False)
+            event.extraTests.append(suite)
+        elif not loader.VALID_MODULE_NAME.match(os.path.basename(path)):
+            return
+        name = loader_._get_name_from_path(path)
+        try:
+            module = loader_._get_module_from_name(name)
+        except:
+            return
+        if hasattr(module, '__test__') and not module.__test__:
+            return
+        try:
+            suite = doctest.DocTestSuite(module)
+        except ValueError:
+            # doctest, very annoyingly, raises ValueError when
+            # a module has no tests.
+            return
+        event.extraTests.append(suite)
+>>> 1 == 1
+True
+>>> 2 == 2
+True
+>>> 3 == 2
+False

support/pkg1/mod1.py

+
+def some_other_func():
+    """This is a function with an inline doctest.
+
+    >>> a = 1
+    >>> b = 2
+    >>> a == b
+    False
+    """
+    pass