summaryrefslogtreecommitdiff
path: root/python/helpers/pydev/pydev_runfiles_unittest.py
blob: 78dfa524cf406d573fc1a06e1ef450f08acc9de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
try:
    import unittest2 as python_unittest
except:
    import unittest as python_unittest
    
import pydev_runfiles_xml_rpc
import time
import pydevd_io
import traceback
from pydevd_constants import * #@UnusedWildImport

    
#=======================================================================================================================
# PydevTextTestRunner
#=======================================================================================================================
class PydevTextTestRunner(python_unittest.TextTestRunner):
    
    def _makeResult(self):
        return PydevTestResult(self.stream, self.descriptions, self.verbosity)


_PythonTextTestResult = python_unittest.TextTestRunner()._makeResult().__class__

#=======================================================================================================================
# PydevTestResult
#=======================================================================================================================
class PydevTestResult(_PythonTextTestResult):
    

    def startTest(self, test):
        _PythonTextTestResult.startTest(self, test)
        self.buf = pydevd_io.StartRedirect(keep_original_redirection=True, std='both')
        self.start_time = time.time()
        self._current_errors_stack = []
        self._current_failures_stack = []
        
        try:
            test_name = test.__class__.__name__+"."+test._testMethodName
        except AttributeError:
            #Support for jython 2.1 (__testMethodName is pseudo-private in the test case)
            test_name = test.__class__.__name__+"."+test._TestCase__testMethodName
            
        pydev_runfiles_xml_rpc.notifyStartTest(
            test.__pydev_pyfile__, test_name)




    def getTestName(self, test):
        try:
            try:
                test_name = test.__class__.__name__ + "." + test._testMethodName
            except AttributeError:
                #Support for jython 2.1 (__testMethodName is pseudo-private in the test case)
                try:
                    test_name = test.__class__.__name__ + "." + test._TestCase__testMethodName
                #Support for class/module exceptions (test is instance of _ErrorHolder)
                except:
                    test_name = test.description.split()[1][1:-1] + ' <' + test.description.split()[0] + '>'
        except:
            traceback.print_exc()
            return '<unable to get test name>'
        return test_name


    def stopTest(self, test):
        end_time = time.time()
        pydevd_io.EndRedirect(std='both')
        
        _PythonTextTestResult.stopTest(self, test)
        
        captured_output = self.buf.getvalue()
        del self.buf
        error_contents = ''
        test_name = self.getTestName(test)
            
        
        diff_time = '%.2f' % (end_time - self.start_time)
        if not self._current_errors_stack and not self._current_failures_stack:
            pydev_runfiles_xml_rpc.notifyTest(
                'ok', captured_output, error_contents, test.__pydev_pyfile__, test_name, diff_time)
        else:
            self._reportErrors(self._current_errors_stack, self._current_failures_stack, captured_output, test_name)
            
            
    def _reportErrors(self, errors, failures, captured_output, test_name, diff_time=''):
        error_contents = []
        for test, s in errors+failures:
            if type(s) == type((1,)): #If it's a tuple (for jython 2.1)
                sio = StringIO()
                traceback.print_exception(s[0], s[1], s[2], file=sio)
                s = sio.getvalue()
            error_contents.append(s)
        
        sep = '\n'+self.separator1
        error_contents = sep.join(error_contents)
        
        if errors and not failures:
            try:
                pydev_runfiles_xml_rpc.notifyTest(
                    'error', captured_output, error_contents, test.__pydev_pyfile__, test_name, diff_time)
            except:
                file_start = error_contents.find('File "')
                file_end = error_contents.find('", ', file_start)
                if file_start != -1 and file_end != -1:
                    file = error_contents[file_start+6:file_end]
                else:
                    file = '<unable to get file>'
                pydev_runfiles_xml_rpc.notifyTest(
                    'error', captured_output, error_contents, file, test_name, diff_time)
            
        elif failures and not errors:
            pydev_runfiles_xml_rpc.notifyTest(
                'fail', captured_output, error_contents, test.__pydev_pyfile__, test_name, diff_time)
        
        else: #Ok, we got both, errors and failures. Let's mark it as an error in the end.
            pydev_runfiles_xml_rpc.notifyTest(
                'error', captured_output, error_contents, test.__pydev_pyfile__, test_name, diff_time)
                


    def addError(self, test, err):
        _PythonTextTestResult.addError(self, test, err)
        #Support for class/module exceptions (test is instance of _ErrorHolder)
        if not hasattr(self, '_current_errors_stack') or test.__class__.__name__ == '_ErrorHolder':
            #Not in start...end, so, report error now (i.e.: django pre/post-setup)
            self._reportErrors([self.errors[-1]], [], '', self.getTestName(test))
        else:
            self._current_errors_stack.append(self.errors[-1])


    def addFailure(self, test, err):
        _PythonTextTestResult.addFailure(self, test, err)
        if not hasattr(self, '_current_failures_stack'):
            #Not in start...end, so, report error now (i.e.: django pre/post-setup)
            self._reportErrors([], [self.failures[-1]], '', self.getTestName(test))
        else:
            self._current_failures_stack.append(self.failures[-1])


try:
    #Version 2.7 onwards has a different structure... Let's not make any changes in it for now
    #(waiting for bug: http://bugs.python.org/issue11798)
    try:
        from unittest2 import suite
    except ImportError:
        from unittest import suite
    #===================================================================================================================
    # PydevTestSuite
    #===================================================================================================================
    class PydevTestSuite(python_unittest.TestSuite):
        pass
    
    
except ImportError:
    
    #===================================================================================================================
    # PydevTestSuite
    #===================================================================================================================
    class PydevTestSuite(python_unittest.TestSuite):
    
    
        def run(self, result):
            for index, test in enumerate(self._tests):
                if result.shouldStop:
                    break
                test(result)
    
                # Let the memory be released! 
                self._tests[index] = None
    
            return result