summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Collins <robertc@robertcollins.net>2005-11-30 22:48:34 +1100
committerRobert Collins <robertc@robertcollins.net>2005-11-30 22:48:34 +1100
commit71e63762cb62b0a1025b6087beda897417a3a8fc (patch)
treefa57115b74d68e33d2e3f4e3241b7935650f9464
parent6f35f6f145cac7e6e2796bafad3705021a5dc481 (diff)
downloadsubunit-git-71e63762cb62b0a1025b6087beda897417a3a8fc.tar.gz
Implement IsolatedTestSuite.
-rw-r--r--README1
-rw-r--r--lib/subunit/__init__.py79
-rw-r--r--lib/subunit/tests/test_test_protocol.py36
3 files changed, 83 insertions, 33 deletions
diff --git a/README b/README
index a8fe59e..7e77794 100644
--- 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
diff --git a/lib/subunit/__init__.py b/lib/subunit/__init__.py
index 69b0e76..69f8736 100644
--- a/lib/subunit/__init__.py
+++ b/lib/subunit/__init__.py
@@ -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
diff --git a/lib/subunit/tests/test_test_protocol.py b/lib/subunit/tests/test_test_protocol.py
index ecd1cdf..8971723 100644
--- a/lib/subunit/tests/test_test_protocol.py
+++ b/lib/subunit/tests/test_test_protocol.py
@@ -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):