Implement IsolatedTestSuite.
authorRobert Collins <robertc@robertcollins.net>
Wed, 30 Nov 2005 11:48:34 +0000 (22:48 +1100)
committerRobert Collins <robertc@robertcollins.net>
Wed, 30 Nov 2005 11:48:34 +0000 (22:48 +1100)
README
lib/subunit/__init__.py
lib/subunit/tests/test_test_protocol.py

diff --git a/README b/README
index a8fe59e344fe9f1f9d094f2c7611ed59905101b5..7e77794447f8d4f3e63e77a805c8c3ceef537d7c 100644 (file)
--- a/README
+++ b/README
@@ -81,7 +81,6 @@ class TestFoo(subunit.IsolatedTestCase):
 
 3) As a wrapper around a TestCase to run a group of tests externally.
 =====================================================================
-This hasn't been implemented yet, but it will look something like:
 
 import subunit
 import unittest
index 69b0e766df3a74e2555c580b7141672fcfd07d4f..69f873654e6e7abcb47cbb134a76ce8cb3a20146 100644 (file)
@@ -314,35 +314,50 @@ class IsolatedTestCase(unittest.TestCase):
 
     def run(self, result=None):
         if result is None: result = self.defaultTestResult()
-        c2pread, c2pwrite = os.pipe()
-        # fixme - error -> result
-        # now fork
-        pid = os.fork()
-        if pid == 0:
-            # Child
-            # Close parent's pipe ends
-            os.close(c2pread)
-            # Dup fds for child
-            os.dup2(c2pwrite, 1)
-            # Close pipe fds.
-            os.close(c2pwrite)
-
-            # at this point, sys.stdin is redirected, now we want
-            # to filter it to escape ]'s.
-            ### XXX: test and write that bit.
-
-            result = TestProtocolClient(sys.stdout)
-            unittest.TestCase.run(self, result)
-            sys.stdout.flush()
-            sys.stderr.flush()
-            # exit HARD, exit NOW.
-            os._exit(0)
-        else:
-            # Parent
-            # Close child pipe ends
-            os.close(c2pwrite)
-            # hookup a protocol engine
-            protocol = TestProtocolServer(result)
-            protocol.readFrom(os.fdopen(c2pread, 'rU'))
-            os.waitpid(pid, 0)
-            # TODO return code evaluation.
+        run_isolated(unittest.TestCase, self, result)
+
+
+class IsolatedTestSuite(unittest.TestSuite):
+    """A TestCase which runs its tests in a forked process."""
+
+    def run(self, result=None):
+        if result is None: result = unittest.TestResult()
+        run_isolated(unittest.TestSuite, self, result)
+
+
+def run_isolated(klass, self, result):
+    """Run a test suite or case in a subprocess, using the run method on klass.
+    """
+    c2pread, c2pwrite = os.pipe()
+    # fixme - error -> result
+    # now fork
+    pid = os.fork()
+    if pid == 0:
+        # Child
+        # Close parent's pipe ends
+        os.close(c2pread)
+        # Dup fds for child
+        os.dup2(c2pwrite, 1)
+        # Close pipe fds.
+        os.close(c2pwrite)
+    
+        # at this point, sys.stdin is redirected, now we want
+        # to filter it to escape ]'s.
+        ### XXX: test and write that bit.
+    
+        result = TestProtocolClient(sys.stdout)
+        klass.run(self, result)
+        sys.stdout.flush()
+        sys.stderr.flush()
+        # exit HARD, exit NOW.
+        os._exit(0)
+    else:
+        # Parent
+        # Close child pipe ends
+        os.close(c2pwrite)
+        # hookup a protocol engine
+        protocol = TestProtocolServer(result)
+        protocol.readFrom(os.fdopen(c2pread, 'rU'))
+        os.waitpid(pid, 0)
+        # TODO return code evaluation.
+    return result
index ecd1cdf2992618ec9035af59235243d15eb01bcb..8971723b4751fb35ea50adbe31df2f340148b67f 100644 (file)
@@ -636,6 +636,42 @@ class TestIsolatedTestCase(unittest.TestCase):
         #test.debug()
 
 
+class TestIsolatedTestSuite(unittest.TestCase):
+    
+    class SampleTestToIsolate(unittest.TestCase):
+
+        SETUP = False
+        TEARDOWN = False
+        TEST = False
+
+        def setUp(self):
+            TestIsolatedTestSuite.SampleTestToIsolate.SETUP = True
+            
+        def tearDown(self):
+            TestIsolatedTestSuite.SampleTestToIsolate.TEARDOWN = True
+
+        def test_sets_global_state(self):
+            TestIsolatedTestSuite.SampleTestToIsolate.TEST = True
+
+
+    def test_construct(self):
+        suite = subunit.IsolatedTestSuite()
+
+    def test_run(self):
+        result = unittest.TestResult()
+        suite = subunit.IsolatedTestSuite()
+        sub_suite = unittest.TestSuite()
+        sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
+        sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
+        suite.addTest(sub_suite)
+        suite.addTest(self.SampleTestToIsolate("test_sets_global_state"))
+        suite.run(result)
+        self.assertEqual(result.testsRun, 3)
+        self.assertEqual(self.SampleTestToIsolate.SETUP, False)
+        self.assertEqual(self.SampleTestToIsolate.TEARDOWN, False)
+        self.assertEqual(self.SampleTestToIsolate.TEST, False)
+
+
 class TestTestProtocolClient(unittest.TestCase):
 
     def setUp(self):