DebugClients/Python/DebugClientBase.py

changeset 6901
f2c774c8db7e
parent 6899
8c4cf9c405c7
child 6904
3f35037a08d4
diff -r 060a30488316 -r f2c774c8db7e DebugClients/Python/DebugClientBase.py
--- a/DebugClients/Python/DebugClientBase.py	Mon Mar 25 20:18:47 2019 +0100
+++ b/DebugClients/Python/DebugClientBase.py	Tue Mar 26 19:29:30 2019 +0100
@@ -801,6 +801,47 @@
         elif method == "RequestCompletion":
             self.__completionList(params["text"])
         
+        elif method == "RequestUTDiscover":
+            if params["syspath"]:
+                sys.path = params["syspath"] + sys.path
+            
+            discoveryStart = params["discoverystart"]
+            if not discoveryStart:
+                discoveryStart = params["workdir"]
+            
+            os.chdir(params["discoverystart"])
+            
+            # set the system exception handling function to ensure, that
+            # we report on all unhandled exceptions
+            sys.excepthook = self.__unhandled_exception
+            self.__interceptSignals()
+            
+            try:
+                import unittest
+                testLoader = unittest.TestLoader()
+                test = testLoader.discover(discoveryStart)
+                if hasattr(testLoader, "errors") and \
+                   bool(testLoader.errors):
+                    self.sendJsonCommand("ResponseUTDiscover", {
+                        "testCasesList": [],
+                        "exception": "DiscoveryError",
+                        "message": "\n\n".join(testLoader.errors),
+                    })
+                else:
+                    testsList = self.__assembleTestCasesList(test)
+                    self.sendJsonCommand("ResponseUTDiscover", {
+                        "testCasesList": testsList,
+                        "exception": "",
+                        "message": "",
+                    })
+            except Exception:
+                exc_type, exc_value, exc_tb = sys.exc_info()
+                self.sendJsonCommand("ResponseUTDiscover", {
+                    "testCasesList": [],
+                    "exception": exc_type.__name__,
+                    "message": str(exc_value),
+                })
+        
         elif method == "RequestUTPrepare":
             if params["syspath"]:
                 sys.path = params["syspath"] + sys.path
@@ -818,12 +859,16 @@
             
             try:
                 import unittest
+                testLoader = unittest.TestLoader()
                 if params["discover"]:
                     discoveryStart = params["discoverystart"]
                     if not discoveryStart:
                         discoveryStart = params["workdir"]
-                    self.test = unittest.defaultTestLoader.discover(
-                        discoveryStart)
+                    if params["testcases"]:
+                        self.test = testLoader.loadTestsFromNames(
+                            params["testcases"])
+                    else:
+                        self.test = testLoader.discover(discoveryStart)
                 else:
                     if params["filename"]:
                         utModule = imp.load_source(
@@ -836,18 +881,17 @@
                                       for t in params["failed"]]
                         else:
                             failed = params["failed"][:]
-                        self.test = unittest.defaultTestLoader\
-                            .loadTestsFromNames(failed, utModule)
+                        self.test = testLoader.loadTestsFromNames(
+                            failed, utModule)
                     else:
-                        self.test = unittest.defaultTestLoader\
-                            .loadTestsFromName(params["testfunctionname"],
-                                               utModule)
+                        self.test = testLoader.loadTestsFromName(
+                            params["testfunctionname"], utModule)
             except Exception:
                 exc_type, exc_value, exc_tb = sys.exc_info()
                 self.sendJsonCommand("ResponseUTPrepared", {
                     "count": 0,
                     "exception": exc_type.__name__,
-                    "message": str(exc_value) + "<br/>" + str(params),
+                    "message": str(exc_value),
                 })
                 return
             
@@ -888,6 +932,30 @@
             self.fork_child = (params["target"] == 'child')
             self.eventExit = True
     
+    def __assembleTestCasesList(self, suite):
+        """
+        Private method to assemble a list of test cases included in a test
+        suite.
+        
+        @param suite test suite to be inspected
+        @type unittest.TestSuite
+        @return list of tuples containing the test case ID and short
+            description
+        @rtype list of tuples of (str, str)
+        """
+        import unittest
+        testCases = []
+        for test in suite:
+            if isinstance(test, unittest.TestSuite):
+                testCases.extend(self.__assembleTestCasesList(test))
+            else:
+                testId = test.id()
+                if "ModuleImportFailure" not in testId and \
+                   "LoadTestsFailure" not in testId and \
+                   "_FailedTest" not in testId:
+                    testCases.append((test.id(), test.shortDescription()))
+        return testCases
+    
     def sendJsonCommand(self, method, params):
         """
         Public method to send a single command or response to the IDE.

eric ide

mercurial