OLD | NEW |
| (Empty) |
1 #!/usr/bin/python2.4 | |
2 # Copyright 2009, Google Inc. | |
3 # All rights reserved. | |
4 # | |
5 # Redistribution and use in source and binary forms, with or without | |
6 # modification, are permitted provided that the following conditions are | |
7 # met: | |
8 # | |
9 # * Redistributions of source code must retain the above copyright | |
10 # notice, this list of conditions and the following disclaimer. | |
11 # * Redistributions in binary form must reproduce the above | |
12 # copyright notice, this list of conditions and the following disclaimer | |
13 # in the documentation and/or other materials provided with the | |
14 # distribution. | |
15 # * Neither the name of Google Inc. nor the names of its | |
16 # contributors may be used to endorse or promote products derived from | |
17 # this software without specific prior written permission. | |
18 # | |
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | |
31 | |
32 """Test runner that displays customized results for pulse. | |
33 | |
34 Instead of the usual '.', 'E', and 'F' symbols, more detailed | |
35 results are printed after each test. | |
36 | |
37 ie. 'TESTRESULT $TESTNAME: {PASS/FAIL}' | |
38 """ | |
39 | |
40 | |
41 | |
42 import sys | |
43 import time | |
44 import unittest | |
45 | |
46 | |
47 # Disable lint errors about functions having to start with upper case. | |
48 # This is done to standardize the case of all functions in this class. | |
49 # pylint: disable-msg=C6409 | |
50 class _PulseTestResult(unittest.TestResult): | |
51 """A specialized class that prints formatted text results to a stream. | |
52 | |
53 Test results are formatted to be recognized in pulse. | |
54 Used by PulseTestRunner. | |
55 """ | |
56 separator1 = "=" * 70 | |
57 separator2 = "-" * 70 | |
58 | |
59 def __init__(self, stream, descriptions, verbosity, browser): | |
60 unittest.TestResult.__init__(self) | |
61 self.stream = stream | |
62 self.show_all = verbosity > 1 | |
63 self.dots = verbosity == 1 | |
64 self.descriptions = descriptions | |
65 # Dictionary of start times | |
66 self.start_times = {} | |
67 # Dictionary of results | |
68 self.results = {} | |
69 self.browser = browser | |
70 | |
71 def getDescription(self, test): | |
72 """Gets description of test.""" | |
73 if self.descriptions: | |
74 return test.shortDescription() or str(test) | |
75 else: | |
76 return str(test) | |
77 | |
78 def startTest(self, test): | |
79 """Starts test.""" | |
80 # Records the start time | |
81 self.start_times[test] = time.time() | |
82 # Default testresult if success not called | |
83 self.results[test] = "FAIL" | |
84 unittest.TestResult.startTest(self, test) | |
85 if self.show_all: | |
86 self.stream.write(self.getDescription(test)) | |
87 self.stream.write(" ... ") | |
88 | |
89 def stopTest(self, test): | |
90 """Called when test is ended.""" | |
91 time_taken = time.time() - self.start_times[test] | |
92 result = self.results[test] | |
93 self.stream.writeln("SELENIUMRESULT %s <%s> [%.3fs]: %s" | |
94 % (test, self.browser, time_taken, result)) | |
95 | |
96 def addSuccess(self, test): | |
97 """Adds success result to TestResult.""" | |
98 unittest.TestResult.addSuccess(self, test) | |
99 self.results[test] = "PASS" | |
100 | |
101 def addError(self, test, err): | |
102 """Adds error result to TestResult.""" | |
103 unittest.TestResult.addError(self, test, err) | |
104 self.results[test] = "FAIL" | |
105 | |
106 def addFailure(self, test, err): | |
107 """Adds failure result to TestResult.""" | |
108 unittest.TestResult.addFailure(self, test, err) | |
109 self.results[test] = "FAIL" | |
110 | |
111 def printErrors(self): | |
112 """Prints all errors and failures.""" | |
113 if self.dots or self.show_all: | |
114 self.stream.writeln() | |
115 self.printErrorList("ERROR", self.errors) | |
116 self.printErrorList("FAIL", self.failures) | |
117 | |
118 def printErrorList(self, flavour, errors): | |
119 """Prints a given list of errors.""" | |
120 for test, err in errors: | |
121 self.stream.writeln(self.separator1) | |
122 self.stream.writeln("%s: %s" % (flavour, self.getDescription(test))) | |
123 self.stream.writeln(self.separator2) | |
124 self.stream.writeln("%s" % err) | |
125 | |
126 | |
127 class PulseTestRunner(unittest.TextTestRunner): | |
128 """A specialized test runner class that displays results in textual form. | |
129 | |
130 Test results are formatted to be recognized in pulse. | |
131 """ | |
132 | |
133 def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1): | |
134 self.browser = "default_browser" | |
135 unittest.TextTestRunner.__init__(self, stream, descriptions, verbosity) | |
136 | |
137 def setBrowser(self, browser): | |
138 """Sets the browser name.""" | |
139 self.browser = browser | |
140 | |
141 def _makeResult(self): | |
142 """Returns a formatted test result for pulse.""" | |
143 return _PulseTestResult(self.stream, | |
144 self.descriptions, | |
145 self.verbosity, | |
146 self.browser) | |
147 | |
148 if __name__ == "__main__": | |
149 pass | |
OLD | NEW |