Arve Knudsen avatar Arve Knudsen committed 66aabf9

Add tests for plugin testid

Comments (0)

Files changed (2)

nose2/tests/_common.py

+"""Common functionality for tests."""
+class _FakeEventBase(object):
+    """Baseclass for fake Events."""
+    def __init__(self):
+        self._fake_messages = []
+
+    def message(self, msg, verbosity=(1, 2)):
+        self._fake_messages.append((msg, verbosity))
+
+class FakeStartTestEvent(_FakeEventBase):
+    """Fake StartTestEvent."""
+    def __init__(self, test):
+        super(FakeStartTestEvent, self).__init__()
+        self.test = test
+        self.result = test.defaultTestResult()
+        import time
+        self.startTime = time.time()
+
+class FakeLoadFromNameEvent(_FakeEventBase):
+    """Fake LoadFromNameEvent."""
+    def __init__(self, name):
+        super(FakeLoadFromNameEvent, self).__init__()
+        self.name = name

nose2/tests/test_testid_plugin.py

+"""Test testid plugin."""
+from unittest2 import TestCase
+import re
+import os.path
+import pickle
+import shutil
+import tempfile
+
+from ..plugins import testid
+from ._common import FakeStartTestEvent, FakeLoadFromNameEvent
+
+class UnitTestTestId(TestCase):
+    """Test class TestId.
+    
+    Tests are carried out in a temporary directory, since TestId stores state
+    to file. The temporary directory is removed after testing.
+    """
+    tags = ['unit']
+
+    def setUp(self):
+        super(UnitTestTestId, self).setUp()
+        self.__orig_dir = os.getcwd()
+        # XXX: Use deterministic temporary directory, which can be inspected
+        # after test?
+        self.__temp_dir = tempfile.mkdtemp()
+        os.chdir(self.__temp_dir)
+
+    def tearDown(self):
+        super(UnitTestTestId, self).tearDown()
+        os.chdir(self.__orig_dir)
+        shutil.rmtree(self.__temp_dir, ignore_errors=True)
+
+
+    def test___init__(self):
+        """Test the __init__ method."""
+        plug = self.__create()
+        # Test attributes
+        for name, exp_val in [('configSection', 'testid'), ('commandLineSwitch',
+            ('I', 'with-id', 'Add test ids to output')), ('idfile',
+                os.path.abspath('.noseids')), ('ids', {}), ('tests', {}),
+                ('id', 0)]:
+            try:
+                val = getattr(plug, name)
+            except AttributeError:
+                self.fail('TestId instance doesn\'t have attribute %s' % (name,))
+            self.assertEqual(val, exp_val, 'Attribute %s should have value '
+                '\'%s\', but has value %s' % (name, exp_val, val))
+
+
+    def test_start_test(self):
+        """Test startTest method."""
+        event = FakeStartTestEvent(self)
+        plug = self.__create()
+        plug.startTest(event)
+
+        self.assertEqual(plug.id, 1)
+        test_id = self.id()
+        self.assertEqual(plug.ids, {1: test_id})
+        self.assertEqual(plug.tests, {test_id: 1})
+        self.assertEqual(event._fake_messages, [('#1 ', (2,))])
+
+    def test_start_test_twice(self):
+        """Test calling startTest twice."""
+        event = FakeStartTestEvent(self)
+        plug = self.__create()
+        plug.startTest(event)
+        plug.startTest(event)
+
+        self.assertEqual(plug.id, 1)
+        test_id = self.id()
+        self.assertEqual(plug.ids, {1: test_id})
+        self.assertEqual(plug.tests, {test_id: 1})
+        msg = ('#1 ', (2,))
+        self.assertEqual(event._fake_messages, [msg, msg])
+
+
+    def test_stop_test_run(self):
+        """Test stopTestRun method."""
+        plug = self.__create()
+        plug.startTest(FakeStartTestEvent(self))
+        plug.stopTestRun(None)
+
+        fh = open(plug.idfile, 'r')
+        try:
+            data = pickle.load(fh)
+        finally:
+            fh.close()
+        self.assertEqual(data, {'ids': plug.ids, 'tests': plug.tests})
+
+
+    def test_load_tests_from_name(self):
+        """Test loadTestsFromName method."""
+        plug = self.__create()
+        # By first starting/stopping a test, an ID is assigned by the plugin
+        plug.startTest(FakeStartTestEvent(self))
+        plug.stopTestRun(None)
+        event = FakeLoadFromNameEvent('1')
+        plug.loadTestsFromName(event)
+
+        # The numeric ID should be translated to one of our test methods
+        cls_name, mthd_name = event.name.split('.')[-2:]
+        self.assertEqual(cls_name, type(self).__name__)
+        self.assertTrue(mthd_name.startswith('test_'))
+
+    def test_load_tests_from_name_no_ids(self):
+        """Test calling loadTestsFromName when no IDs have been saved."""
+        plug = self.__create()
+        event = FakeLoadFromNameEvent('1')
+        plug.loadTestsFromName(event)
+        # The event's name should be unchanged, since no IDs should be mapped
+        self.assertEqual(event.name, '1')
+
+
+    def __create(self):
+        """Create a TestId instance."""
+        return testid.TestId()
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.