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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#
# Copyright (c) 2009, 2010 Testrepository Contributors
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# license you chose for the specific language governing permissions and
# limitations under that license.
"""In memory storage of test results."""
from io import BytesIO
import subunit
from testtools.content import TracebackContent
from testrepository.repository import (
AbstractRepository,
AbstractRepositoryFactory,
AbstractTestRun,
RepositoryNotFound,
)
class RepositoryFactory(AbstractRepositoryFactory):
"""A factory that can initialise and open memory repositories.
This is used for testing where a repository may be created and later
opened, but tests should not see each others repositories.
"""
def __init__(self):
self.repos = {}
def initialise(self, url):
self.repos[url] = Repository()
return self.repos[url]
def open(self, url):
try:
return self.repos[url]
except KeyError:
raise RepositoryNotFound(url)
class Repository(AbstractRepository):
"""In memory storage of test results."""
def __init__(self):
# Test runs:
self._runs = []
self._failing = {} # id -> test
self._times = {} # id -> duration
def count(self):
return len(self._runs)
def get_failing(self):
return _Failures(self)
def get_test_run(self, run_id):
if run_id < 0:
raise KeyError("No such run.")
return self._runs[run_id]
def latest_id(self):
result = self.count() - 1
if result < 0:
raise KeyError("No tests in repository")
return result
def _get_inserter(self, partial):
return _Inserter(self, partial)
def _get_test_times(self, test_ids):
result = {}
for test_id in test_ids:
duration = self._times.get(test_id, None)
if duration is not None:
result[test_id] = duration
return result
# XXX: Too much duplication between this and _Inserter
class _Failures(AbstractTestRun):
"""Report on failures from a memory repository."""
def __init__(self, repository):
self._repository = repository
def get_id(self):
return None
def get_subunit_stream(self):
result = BytesIO()
serialiser = subunit.TestProtocolClient(result)
self.run(serialiser)
result.seek(0)
return result
def get_test(self):
return self
def run(self, result):
for outcome, test, details in self._repository._failing.values():
result.startTest(test)
getattr(result, 'add' + outcome)(test, details=details)
result.stopTest(test)
class _Inserter(AbstractTestRun):
"""Insert test results into a memory repository, and describe them later."""
def __init__(self, repository, partial):
self._repository = repository
self._partial = partial
self._outcomes = []
self._events = []
self._time = None
self._test_start = None
def startTestRun(self):
pass
def stopTestRun(self):
self._repository._runs.append(self)
self._run_id = len(self._repository._runs) - 1
if not self._partial:
self._repository._failing = {}
for record in self._outcomes:
test_id = record[1].id()
if record[0] in ('Failure', 'Error'):
self._repository._failing[test_id] = record
else:
self._repository._failing.pop(test_id, None)
return self._run_id
def startTest(self, test):
self._test_start = self._time
self._events.append(('startTest', test))
def stopTest(self, test):
self._events.append(('stopTest', test))
if None in (self._test_start, self._time):
return
duration_delta = self._time - self._test_start
duration_seconds = ((duration_delta.microseconds +
(duration_delta.seconds + duration_delta.days * 24 * 3600)
* 10**6) / 10.0**6)
self._repository._times[test.id()] = duration_seconds
def _addOutcome(self, outcome, test, details):
self._outcomes.append((outcome, test, details))
def addSuccess(self, test, details=None):
self._events.append(('addSuccess', test, details))
self._addOutcome('Success', test, details)
def _force_to_details(self, test, err, details):
if not details:
details = {}
if err is not None:
details['err'] = TracebackContent(err, test)
return details
def addFailure(self, test, err=None, details=None):
# Don't support old interface for now.
self._events.append(('addFailure', test, err, details))
details = self._force_to_details(test, err, details)
self._addOutcome('Failure', test, details)
def addError(self, test, err=None, details=None):
self._events.append(('addError', test, err, details))
details = self._force_to_details(test, err, details)
self._addOutcome('Error', test, details)
def addExpectedFailure(self, test, err=None, details=None):
assert err is None
self._events.append(('addExpectedFailure', test, None, details))
self._addOutcome('ExpectedFailure', test, details)
def addUnexpectedSuccess(self, test, details=None):
self._events.append(('addUnexpectedSuccess', test, details))
self._addOutcome('UnexpectedSuccess', test, details)
def addSkip(self, test, reason=None, details=None):
assert reason is None
self._events.append(('addSkip', test, None, details))
self._addOutcome('Skip', test, details)
def get_id(self):
return self._run_id
def get_subunit_stream(self):
result = BytesIO()
serialiser = subunit.TestProtocolClient(result)
self.run(serialiser)
result.seek(0)
return result
def get_test(self):
return self
def run(self, result):
for event in self._events:
method = getattr(result, event[0])
method(*event[1:])
def time(self, timestamp):
self._events.append(('time', timestamp))
self._time = timestamp
|