addError call matches unittest api
authorRobert Collins <robertc@robertcollins.net>
Sat, 27 Aug 2005 23:39:23 +0000 (09:39 +1000)
committerRobert Collins <robertc@robertcollins.net>
Sat, 27 Aug 2005 23:39:23 +0000 (09:39 +1000)
lib/subunit/__init__.py
lib/subunit/tests/test_test_protocol.py

index 2622d19770388a38f118fe85b997833f65607a12..5dad44c1e9cd8c0cf81acfe6a328358c0aefd3ae 100644 (file)
@@ -40,7 +40,7 @@ class TestProtocolServer(object):
             self.current_test_description == line[offset:-1]):
             self.state = TestProtocolServer.OUTSIDE_TEST
             self.current_test_description = None
-            self.addError("")
+            self.addError(self._current_test, RemoteError(""))
         elif (self.state == TestProtocolServer.TEST_STARTED and
             self.current_test_description + " [" == line[offset:-1]):
             self.state = TestProtocolServer.READING_ERROR
@@ -86,7 +86,7 @@ class TestProtocolServer(object):
         elif self.state == TestProtocolServer.READING_ERROR:
             self.state = TestProtocolServer.OUTSIDE_TEST
             self.current_test_description = None
-            self.addError(self._message)
+            self.addError(self._current_test, RemoteError(self._message))
         else:
             self.stdOutLineRecieved(line)
         
@@ -127,16 +127,24 @@ class TestProtocolServer(object):
     def lostConnection(self):
         """The input connection has finished."""
         if self.state == TestProtocolServer.TEST_STARTED:
-            self.addError("lost connection during test '%s'" 
-                          % self.current_test_description)
+            self.addError(self._current_test,
+                          RemoteError("lost connection during test '%s'"
+                                      % self.current_test_description))
         elif self.state == TestProtocolServer.READING_ERROR:
-            self.addError("lost connection during "
-                          "error report of test "
-                          "'%s'" % self.current_test_description)
+            self.addError(self._current_test,
+                          RemoteError("lost connection during "
+                                      "error report of test "
+                                      "'%s'" % self.current_test_description))
         elif self.state == TestProtocolServer.READING_FAILURE:
-            self.addError("lost connection during "
-                          "failure report of test "
-                          "'%s'" % self.current_test_description)
+            self.addError(self._current_test,
+                          RemoteError("lost connection during "
+                                      "failure report of test "
+                                      "'%s'" % self.current_test_description))
+
+    def readFrom(self, pipe):
+        for line in pipe.readlines(pipe):
+            self.lineReceived(line)
+        self.lostConnection()
 
     def _startTest(self, offset, line):
         """Internal call to change state machine. Override startTest()."""
@@ -152,10 +160,20 @@ class TestProtocolServer(object):
         sys.stdout.write(line)
 
 
-class RemoteError(Exception):
+class RemoteException(Exception):
     """An exception that occured remotely to python."""
+
+    def __eq__(self, other):
+        try:
+            return self.args == other.args
+        except AttributeError:
+            return False
     
 
+def RemoteError(description):
+    return (RemoteException("RemoteError:\n%s" % description), None, None)
+
+
 class RemotedTestCase(unittest.TestCase):
     """A class to represent test cases run in child processes."""
 
@@ -195,7 +213,7 @@ class RemotedTestCase(unittest.TestCase):
     def run(self, result=None):
         if result is None: result = self.defaultTestResult()
         result.startTest(self)
-        result.addError(self, (RemoteError("Cannot run RemotedTestCases.\n"), None, None))
+        result.addError(self, RemoteError("Cannot run RemotedTestCases.\n"))
         result.stopTest(self)
         
     def _strclass(self):
index aa99213e6754532ae8a24536ca151c2e565c3d92..c9521bc88bcc4c81b1c3c379309d39962786e9d5 100644 (file)
@@ -18,6 +18,7 @@
 #
 
 import unittest
+from StringIO import StringIO
 import subunit
 import sys
 
@@ -32,8 +33,8 @@ try:
             self.success_calls = []
             super(MockTestProtocolServer, self).__init__()
 
-        def addError(self, error):
-            self.error_calls.append(error)
+        def addError(self, test, error):
+            self.error_calls.append((test,error))
 
         def addFailure(self, error):
             self.failure_calls.append(error)
@@ -61,9 +62,12 @@ class TestMockTestProtocolServer(unittest.TestCase):
 
     def test_add_error(self):
         protocol = MockTestProtocolServer()
-        protocol.addError("omg it works")
+        protocol.addError(subunit.RemotedTestCase("old mcdonald"), 
+                          subunit.RemoteError("omg it works"))
         self.assertEqual(protocol.start_calls, [])
-        self.assertEqual(protocol.error_calls, ["omg it works"])
+        self.assertEqual(protocol.error_calls, [(
+                            subunit.RemotedTestCase("old mcdonald"),
+                            subunit.RemoteError("omg it works"))])
         self.assertEqual(protocol.failure_calls, [])
         self.assertEqual(protocol.success_calls, [])
         
@@ -90,6 +94,31 @@ class TestTestImports(unittest.TestCase):
     def test_imports(self):
         from subunit import TestProtocolServer
         from subunit import RemotedTestCase
+        from subunit import RemoteError
+
+
+class TestTestProtocolServerPipe(unittest.TestCase):
+
+    def skip_test_story(self):
+        protocol = MockTestProtocolServer()
+        pipe = StringIO("test old mcdonald\n"
+                        "success old mcdonald\n"
+                        "test bing crosby\n"
+                        "failure bing crosby [\n"
+                        "foo.c:53:ERROR invalid state\n"
+                        "]\n"
+                        "test an error\n"
+                        "error an error\n")
+        protocol.readFrom(pipe)
+        mcdonald = subunit.RemotedTestCase("old mcdonald")
+        bing = subunit.RemotedTestCase("bing crosby")
+        an_error = subunit.RemotedTestCase("an error")
+        self.assertEqual(protocol.start_calls, [mcdonald, bing, an_error])
+        self.assertEqual(protocol.error_calls, [(an_error, RemoteError())])
+        self.assertEqual(protocol.failure_calls, [
+                            (bing,
+                             RemoteError("foo.c:53:ERROR invalid state\n"))])
+        self.assertEqual(protocol.success_calls, [mcdonald])
 
 
 class TestTestProtocolServerStartTest(unittest.TestCase):
@@ -162,9 +191,11 @@ class TestTestProtocolServerPassThrough(unittest.TestCase):
         protocol.lineReceived("test old mcdonald\n")
         protocol.lineReceived("error old mcdonald\n")
         self.keywords_before_test(protocol)
+        test = subunit.RemotedTestCase("old mcdonald")
         self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
-        self.assertEqual(protocol.error_calls, [""])
+                         [test])
+        self.assertEqual(protocol.error_calls, [(test, 
+                                                 subunit.RemoteError(""))])
         self.assertEqual(protocol.failure_calls, [])
         self.assertEqual(protocol.success_calls, [])
         
@@ -267,9 +298,11 @@ class TestTestProtocolServerLostConnection(unittest.TestCase):
         protocol = MockTestProtocolServer()
         protocol.lineReceived("test old mcdonald\n")
         protocol.lostConnection()
-        self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
-        self.assertEqual(protocol.error_calls, ["lost connection during test 'old mcdonald'"])
+        test = subunit.RemotedTestCase("old mcdonald")
+        self.assertEqual(protocol.start_calls, [test])
+        self.assertEqual(protocol.error_calls, [
+            (test, subunit.RemoteError("lost connection during "
+                                       "test 'old mcdonald'"))])
         self.assertEqual(protocol.failure_calls, [])
         self.assertEqual(protocol.success_calls, [])
 
@@ -278,10 +311,11 @@ class TestTestProtocolServerLostConnection(unittest.TestCase):
         protocol.lineReceived("test old mcdonald\n")
         protocol.lineReceived("error old mcdonald\n")
         protocol.lostConnection()
-        self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
+        test = subunit.RemotedTestCase("old mcdonald")
+        self.assertEqual(protocol.start_calls, [test])
         self.assertEqual(protocol.failure_calls, [])
-        self.assertEqual(protocol.error_calls, [""])
+        self.assertEqual(protocol.error_calls, [
+            (test, subunit.RemoteError(""))])
         self.assertEqual(protocol.success_calls, [])
         
     def test_lost_connection_during_error(self):
@@ -289,11 +323,11 @@ class TestTestProtocolServerLostConnection(unittest.TestCase):
         protocol.lineReceived("test old mcdonald\n")
         protocol.lineReceived("error old mcdonald [\n")
         protocol.lostConnection()
-        self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
-        self.assertEqual(protocol.error_calls, ["lost connection during "
-                                                "error report of test "
-                                                "'old mcdonald'"])
+        test = subunit.RemotedTestCase("old mcdonald")
+        self.assertEqual(protocol.start_calls, [test])
+        self.assertEqual(protocol.error_calls, [
+            (test, subunit.RemoteError("lost connection during error report of"
+                                       " test 'old mcdonald'"))])
         self.assertEqual(protocol.failure_calls, [])
         self.assertEqual(protocol.success_calls, [])
 
@@ -302,8 +336,8 @@ class TestTestProtocolServerLostConnection(unittest.TestCase):
         protocol.lineReceived("test old mcdonald\n")
         protocol.lineReceived("failure old mcdonald\n")
         protocol.lostConnection()
-        self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
+        test = subunit.RemotedTestCase("old mcdonald")
+        self.assertEqual(protocol.start_calls, [test])
         self.assertEqual(protocol.error_calls, [])
         self.assertEqual(protocol.failure_calls, [""])
         self.assertEqual(protocol.success_calls, [])
@@ -313,11 +347,11 @@ class TestTestProtocolServerLostConnection(unittest.TestCase):
         protocol.lineReceived("test old mcdonald\n")
         protocol.lineReceived("failure old mcdonald [\n")
         protocol.lostConnection()
-        self.assertEqual(protocol.start_calls,
-                         [subunit.RemotedTestCase("old mcdonald")])
-        self.assertEqual(protocol.error_calls, ["lost connection during "
-                                                "failure report of test "
-                                                "'old mcdonald'"])
+        test = subunit.RemotedTestCase("old mcdonald")
+        self.assertEqual(protocol.start_calls, [test])
+        self.assertEqual(protocol.error_calls, [
+            (test, subunit.RemoteError("lost connection during failure report"
+                                       " of test 'old mcdonald'"))])
         self.assertEqual(protocol.failure_calls, [])
         self.assertEqual(protocol.success_calls, [])
 
@@ -339,12 +373,13 @@ class TestTestProtocolServerAddError(unittest.TestCase):
     def setUp(self):
         self.protocol = MockTestProtocolServer()
         self.protocol.lineReceived("test mcdonalds farm\n")
+        self.test = subunit.RemotedTestCase("mcdonalds farm")
 
     def simple_error_keyword(self, keyword):
         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
-        self.assertEqual(self.protocol.error_calls, [""])
+        self.assertEqual(self.protocol.start_calls, [self.test])
+        self.assertEqual(self.protocol.error_calls, [
+            (self.test, subunit.RemoteError(""))])
         self.assertEqual(self.protocol.failure_calls, [])
 
     def test_simple_error(self):
@@ -356,18 +391,18 @@ class TestTestProtocolServerAddError(unittest.TestCase):
     def test_error_empty_message(self):
         self.protocol.lineReceived("error mcdonalds farm [\n")
         self.protocol.lineReceived("]\n")
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
-        self.assertEqual(self.protocol.error_calls, [""])
+        self.assertEqual(self.protocol.start_calls, [self.test])
+        self.assertEqual(self.protocol.error_calls, [
+            (self.test, subunit.RemoteError(""))])
         self.assertEqual(self.protocol.failure_calls, [])
 
     def error_quoted_bracket(self, keyword):
         self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword)
         self.protocol.lineReceived(" ]\n")
         self.protocol.lineReceived("]\n")
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
-        self.assertEqual(self.protocol.error_calls, ["]\n"])
+        self.assertEqual(self.protocol.start_calls, [self.test])
+        self.assertEqual(self.protocol.error_calls, [
+            (self.test, subunit.RemoteError("]\n"))])
         self.assertEqual(self.protocol.failure_calls, [])
 
     def test_error_quoted_bracket(self):
@@ -382,11 +417,11 @@ class TestTestProtocolServerAddFailure(unittest.TestCase):
     def setUp(self):
         self.protocol = MockTestProtocolServer()
         self.protocol.lineReceived("test mcdonalds farm\n")
+        self.test = subunit.RemotedTestCase("mcdonalds farm")
 
     def simple_failure_keyword(self, keyword):
         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
+        self.assertEqual(self.protocol.start_calls, [self.test])
         self.assertEqual(self.protocol.error_calls, [])
         self.assertEqual(self.protocol.failure_calls, [""])
 
@@ -399,8 +434,7 @@ class TestTestProtocolServerAddFailure(unittest.TestCase):
     def test_failure_empty_message(self):
         self.protocol.lineReceived("failure mcdonalds farm [\n")
         self.protocol.lineReceived("]\n")
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
+        self.assertEqual(self.protocol.start_calls, [self.test])
         self.assertEqual(self.protocol.error_calls, [])
         self.assertEqual(self.protocol.failure_calls, [""])
 
@@ -408,8 +442,7 @@ class TestTestProtocolServerAddFailure(unittest.TestCase):
         self.protocol.lineReceived("%s mcdonalds farm [\n" % keyword)
         self.protocol.lineReceived(" ]\n")
         self.protocol.lineReceived("]\n")
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
+        self.assertEqual(self.protocol.start_calls, [self.test])
         self.assertEqual(self.protocol.error_calls, [])
         self.assertEqual(self.protocol.failure_calls, ["]\n"])
 
@@ -425,14 +458,13 @@ class TestTestProtocolServerAddSuccess(unittest.TestCase):
     def setUp(self):
         self.protocol = MockTestProtocolServer()
         self.protocol.lineReceived("test mcdonalds farm\n")
+        self.test = subunit.RemotedTestCase("mcdonalds farm")
 
     def simple_success_keyword(self, keyword):
         self.protocol.lineReceived("%s mcdonalds farm\n" % keyword)
-        self.assertEqual(self.protocol.start_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
+        self.assertEqual(self.protocol.start_calls, [self.test])
         self.assertEqual(self.protocol.error_calls, [])
-        self.assertEqual(self.protocol.success_calls,
-                         [subunit.RemotedTestCase("mcdonalds farm")])
+        self.assertEqual(self.protocol.success_calls, [self.test])
 
     def test_simple_success(self):
         self.simple_success_keyword("failure")
@@ -462,7 +494,9 @@ class TestRemotedTestCase(unittest.TestCase):
                          "'A test description'>", "%r" % test)
         result = unittest.TestResult()
         test.run(result)
-        self.assertEqual([(test, "Cannot run RemotedTestCases.\n\n")], result.errors)
+        self.assertEqual([(test, "RemoteError:\n"
+                                 "Cannot run RemotedTestCases.\n\n")],
+                         result.errors)
         self.assertEqual(1, result.testsRun)
         another_test = subunit.RemotedTestCase("A test description")
         self.assertEqual(test, another_test)
@@ -471,6 +505,17 @@ class TestRemotedTestCase(unittest.TestCase):
         self.assertNotEqual(another_test, different_test)
 
 
+class TestRemoteError(unittest.TestCase):
+
+    def test_eq(self):
+        error = subunit.RemoteError("Something went wrong")
+        another_error = subunit.RemoteError("Something went wrong")
+        different_error = subunit.RemoteError("boo!")
+        self.assertEqual(error, another_error)
+        self.assertNotEqual(error, different_error)
+        self.assertNotEqual(different_error, another_error)
+
+
 def test_suite():
     loader = subunit.tests.TestUtil.TestLoader()
     result = loader.loadTestsFromName(__name__)