Arve Knudsen avatar Arve Knudsen committed 144b946

Add unittests for doctests plugin

Comments (0)

Files changed (3)

nose2/plugins/doctests.py

         self.extensions = self.config.as_list('extensions', ['.txt', '.rst'])
 
     def handleFile(self, event):
+        """Implement hook."""
         loader_ = event.loader
         path = event.path
         _root, ext = os.path.splitext(path)
             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)

nose2/tests/_common.py

+"""Common functionality."""
+import unittest2.loader
+import os.path
+import tempfile
+import shutil
+
+class TestCase(unittest2.TestCase):
+    """TestCase extension.
+    
+    If the class variable _RUN_IN_TEMP is True (default: False), tests will be
+    performed in a temporary directory, which is deleted afterwards.
+    """
+    _RUN_IN_TEMP = False
+
+    def setUp(self):
+        super(TestCase, self).setUp()
+        
+        if self._RUN_IN_TEMP:
+            self.__orig_dir = os.getcwd()
+            self.__work_dir = tempfile.mkdtemp()
+            os.chdir(self.__work_dir)
+
+    def tearDown(self):
+        super(TestCase, self).tearDown()
+
+        if self._RUN_IN_TEMP:
+            os.chdir(self.__orig_dir)
+            shutil.rmtree(self.__work_dir, ignore_errors=True)
+
+
+class _FakeEventBase(object):
+    """Baseclass for fake Events."""
+
+class FakeHandleFileEvent(_FakeEventBase):
+    """Fake HandleFileEvent."""
+    def __init__(self, name):
+        super(FakeHandleFileEvent, self).__init__()
+
+        self.loader = unittest2.loader.TestLoader()
+        self.name = name
+        self.path = os.path.split(name)[1]
+        self.extraTests = []

nose2/tests/test_doctests.py

+"""Test doctests plugin."""
+import doctest
+
+from ..plugins import doctests
+
+from ._common import TestCase, FakeHandleFileEvent
+
+
+class UnitTestDocTestLoader(TestCase):
+    """Test class DocTestLoader."""
+    tags = ['unit']
+
+    _RUN_IN_TEMP = True
+
+    def test___init__(self):
+        """Test the __init__ method."""
+        plug = self.__create()
+
+        self.assertEqual(plug.extensions, ['.txt', '.rst'])
+
+
+    def test_handle_file(self):
+        """Test method handleFile."""
+        # Create doctest files of supported types
+        plug = self.__create()
+        doc_test = """\
+>>> 2 == 2
+True
+"""
+        for ext in ['txt', 'rst']:
+            fh = open('docs.%s' % ext, 'wb')
+            try:
+                fh.write(doc_test)
+            finally:
+                fh.close()
+
+            event = FakeHandleFileEvent(fh.name)
+            plug.handleFile(event)
+
+            test, = event.extraTests
+            self.assertTrue(isinstance(test, doctest.DocFileCase))
+            self.assertEqual(repr(test), fh.name)
+
+
+    def __create(self):
+        """Create a DocTestLoader instance."""
+        plug = doctests.DocTestLoader()
+        return plug
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.