-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRTM.py
393 lines (308 loc) · 9.44 KB
/
RTM.py
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Purpose: trace which requirements have not been tested.
#
# Usage: python RTM.py
#
# Prerequisites: prepare srs.txt (software requirements specification file) and test.txt (test result file).
# Output is a HTML report named test_report.html.
#
# srs.txt format:
#
# @RQ01
#
# This is my great requirement.
#
#
# test.txt format:
#
# @T001
#
# Test title: test case for requirement 1
#
# Test for: RQ01
#
# Description: None.
#
# Rationale: None.
#
# Input:
#
# Something.
#
# Expected output:
#
# Something else.
#
# Actual output:
#
# As expected.
#
# Diagnosis:
#
# Status: F
#
# Signature: Hui
#
# Date: 2020-03-28
#
#
# License: Apache License v2.0 https://www.apache.org/licenses/LICENSE-2.0.html
#
# Copyright (C) 2020 Hui lanhui at zjnu.edu.cn
import hashlib
import sys, os
import re
COPYRIGHT = 'Copyright (C) 2020 Lan Laboratory [lanhui@zjnu.edu.cn]'
TEST_CASE_START_SYMBOL = '@'
RECOGNIZED_TAGS = ['Test title:', 'Test for:', 'Description:', 'Rationale:', 'Input:', 'Expected output:', 'Actual output:', 'Diagnosis:', 'Status:', 'Signature:', 'Date:']
def get_id(s):
lst = s.split('\n')
line = lst[0]
if line.startswith('@') and len(line) > 1:
return line[1:]
else:
h = hashlib.new('ripemd160')
h.update(s.strip().encode('utf-8'))
return h.hexdigest()
def strip_tag(s):
index = s.find(':')
if index < 0:
return s
else:
return s[index+1:].strip()
def get_other_fields(s):
lst = s.split('\n')
d = {}
if len(lst) < 1:
return d
else:
for x in lst[1:]:
if x != '' and not x.startswith('#'):
flag = 0
for t in RECOGNIZED_TAGS:
if x.lower().startswith(t.lower()):
k = t.lower()
d[k] = strip_tag(x) + '\n'
flag = 1
break
if flag == 0:
d[k] += x + '\n'
return d
def record_test_case(s, d):
if s != '':
test_id = get_id(s)
if not test_id in d:
d[test_id] = get_other_fields(s)
else:
reallybad('Test identifiers identical in test cases: %s.' % (test_id))
return test_id
return None
def read_all_test_cases(fname):
d = {}
test_lst = []
if not os.path.exists(fname):
reallybad('I could not find %s. You must provide a test result file.' % (fname))
f = open(fname, encoding='utf8')
lines = f.readlines()
f.close()
test = ''
for line in lines:
line = line.strip()
if line.startswith('@'):
test_id = record_test_case(test.strip(), d)
if test_id != None:
test_lst.append(test_id)
test = line + '\n'
else:
test += line + '\n'
if test != '':
test_id = record_test_case(test.strip(), d)
if test_id != None:
test_lst.append(test_id)
return test_lst, d
def reallybad(s):
print(s)
sys.exit()
def read_all_requirements(fname):
d = {}
if not os.path.exists(fname):
reallybad('I could not find %s. You must provide a requirements specification file.' % (fname))
f = open(fname, encoding='utf8')
lines = f.readlines()
f.close()
rq_lst = []
k = '?a.really.strange.key.that.should.NEVER.appear!'
for line in lines:
line = line.strip()
if line.startswith('@'):
if len(line) > 1:
k = line[1:]
rq_lst.append(k)
if not k in d:
d[k] = ''
else:
reallybad('Duplicated requirement ID %s.' % (k))
elif k in d:
d[k] += line + '\n'
return rq_lst, d
def pretty(d, indent=0):
for key, value in d.items():
print('\t' * indent + str(key))
if isinstance(value, dict):
pretty(value, indent+1)
else:
print('\t' * (indent+1) + str(value))
def isfail(s):
return s.strip().upper().startswith('F')
def standard_status(s):
s = s.strip().upper()
if s.startswith('F'):
return 'FAILED'
if s.startswith('P'):
return 'PASSED'
return s
def make_html_string_for_string(s):
s = s.strip()
result = ''
for line in s.split('\n'):
result += '%s<br/>' % (line)
return result
def make_html_string_for_dictionary(d):
result = '<table>\n'
for k in RECOGNIZED_TAGS:
key = k.lower()
result += '<tr>\n'
result += '<td>%s</td><td>%s</td>\n' % (k.title(), make_html_string_for_string(d[key]))
result += '</tr>\n'
result += '</table>\n'
return result
def make_plain_string_for_dictionary(d):
result = ''
for k in RECOGNIZED_TAGS:
key = k.lower()
content = d[key]
content = content.replace('"', '[QUOTE]')
content = content.replace('\'', '[QUOTE]')
result += '%s %s\n' % (k.title(), content)
return result
def shorten(k):
len_limit = 6
if len(k) > len_limit:
return k[:len_limit]
return k
def check_requirement_id(id_lst, valid_id_lst):
for x in id_lst:
if not x in valid_id_lst:
reallybad('Identifier %s not in %s.' % (x, ','.join(valid_id_lst)))
def get_first_non_blank_line(s):
lst = s.split('\n')
for line in lst:
line = line.strip()
if line != '':
return line
return 'Warning: completely blank line.'
def generate_matrix(rq_lst, rq_dict, test_lst, test_dict):
requirement_id_lst = rq_lst
s = '''
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<style>
* {font-family: sans-serif !important;}
:target {
background-color: #ffa;
}
</style>
</head>
<body>
'''
s += '<h2 id="matrix">Requirement Traceability Matrix</h2>\n'
s += '<table border=1>\n'
s += '<tr><td>REQUIREMENT\TEST</td>'
for k in test_dict:
s += '<td><a href="#%s" title="%s">%s</a></td>' % (k, k, shorten(k))
s += '</tr>\n'
for r in requirement_id_lst:
s += '<tr>'
s += '<td><a href="#%s" title="%s">%s</a></td>\n' % (r, get_first_non_blank_line(rq_dict[r]), r)
count = 0
for k in test_lst:
if r in test_dict[k]['test for:'].strip().split(', '):
count += 1
for k in test_lst:
rq_lst_in_this_test = test_dict[k]['test for:'].strip().split(', ')
check_requirement_id(rq_lst_in_this_test, requirement_id_lst)
if r in rq_lst_in_this_test:
status_string = standard_status(test_dict[k]['status:'])
if status_string == 'FAILED':
status_string = '<font color="red">%s</font>' % status_string
elif status_string == 'PASSED':
status_string = '<font color="green">%s</font>' % status_string
s += '<td><a title="%s">%s</a></td>\n' % (make_plain_string_for_dictionary(test_dict[k]), status_string)
else:
if count > 0:
s += '<td></td>\n'
else:
s += '<td bgcolor="yellow"></td>\n'
s += '</tr>\n'
s += '</table>\n'
s += '<h2>Requirements</h2>\n'
for r in requirement_id_lst:
s += '<h3 id="%s">%s</h3>\n' % (r, r)
s += make_html_string_for_string(rq_dict[r])
s += '<p><a href="#matrix">Top</a></p>\n'
s += '<h2>Test cases</h2>\n'
for t in test_lst:
s += '<h3 id="%s">%s</h3>\n' % (t, t)
s += make_html_string_for_dictionary(test_dict[t])
s += '<p><a href="#matrix">Top</a></p>\n'
template = '''
<script>
function showDiv() {
document.getElementById('TemplateInformation').style.display = "block";
}
</script>
<input type="button" value="Show format for srs.txt and test.txt" onclick="showDiv()" />
<div id="TemplateInformation" style="display:none;" >
<pre>
=====================
Template for srs.txt
=====================
@RQ01
This is my great requirement.
=====================
Template for test.txt
=====================
@T001
Test title: test case for requirement 1
Test for: RQ01
Description: None.
Rationale: None.
Input:
Something.
Expected output:
Something else.
Actual output:
As expected.
Diagnosis:
Status: F
Signature: Hui
Date: 2020-03-28
</pre>
</div>
'''
report_info = '''
This test report was generated by RTM.py on
<script>
document.write(document.lastModified);
</script>
'''
s += '%s<p><i>%s</i></p><p><i>%s</i></p></body>\n</html>\n' % (template, report_info, COPYRIGHT)
return s
# main
rq_lst, rq_dict = read_all_requirements('srs.txt')
test_lst, test_dict = read_all_test_cases('test.txt')
f = open('test_report.html', 'w')
f.write(generate_matrix(rq_lst, rq_dict, test_lst, test_dict))
f.close()
print('Check test_report.html ...')