9
9
10
10
11
11
# TODO: color stderr
12
- # TODO: simplify javascript using ,ore than 1 class in the class attribute?
12
+ # TODO: simplify javascript using , more than 1 class in the class attribute?
13
13
14
14
15
15
# ------------------------------------------------------------------- #
@@ -102,7 +102,7 @@ class TemplateMixin(object):
102
102
}
103
103
104
104
DEFAULT_TITLE = 'Test Report'
105
- DEFAULT_DESCRIPTION = ''
105
+ DEFAULT_DESCRIPTION = 'This is a simple description. And it has two phrases. '
106
106
107
107
# ------------------------------------------------------------------- #
108
108
# HTML Template
@@ -116,29 +116,29 @@ class TemplateMixin(object):
116
116
# ------------------------------------------------------------------- #
117
117
# alternatively use a <link> for external style sheet, e.g.
118
118
# <link rel="stylesheet" href="$url" type="text/css">
119
- STYLESHEET_TEMPLATE = open ('templates/stylesheet .html' , 'r' ).read () \
119
+ STYLESHEET_TEMPLATE = open ('templates/head_inserts .html' , 'r' ).read () \
120
120
.encode ('utf-8' )
121
121
122
122
# ------------------------------------------------------------------- #
123
123
# Heading
124
124
# ------------------------------------------------------------------- #
125
125
# variables: (title, parameters, description)
126
- HEADING_TEMPLATE = open ('templates/heading .html' , 'r' ).read () \
126
+ HEADING_TEMPLATE = open ('templates/header .html' , 'r' ).read () \
127
127
.encode ('utf-8' )
128
128
129
129
# variables: (name, value)
130
- HEADING_ATTRIBUTE_TEMPLATE = open ('templates/heading_attribute .html' , 'r' )\
130
+ HEADING_ATTRIBUTE_TEMPLATE = open ('templates/header_parameters .html' , 'r' )\
131
131
.read ().encode ('utf-8' )
132
132
133
133
# ------------------------------------------------------------------- #
134
134
# Report
135
135
# ------------------------------------------------------------------- #
136
136
# variables: (test_list, count, Pass, fail, error, skip)
137
- REPORT__TABLE_TEMPLATE = open ('templates/report_table .html' , 'r' ) \
137
+ REPORT__TABLE_TEMPLATE = open ('templates/result_table .html' , 'r' ) \
138
138
.read ().encode ('utf-8' )
139
139
140
140
# variables: (style, desc, count, Pass, fail, error, cid)
141
- REPORT_CLASS_TEMPLATE = open ('templates/report_class .html' , 'r' ).read ()\
141
+ REPORT_CLASS_TEMPLATE = open ('templates/test_class .html' , 'r' ).read ()\
142
142
.encode ('utf-8' )
143
143
144
144
# variables: (tid, Class, style, desc, status)
@@ -157,7 +157,8 @@ class TemplateMixin(object):
157
157
# ------------------------------------------------------------------- #
158
158
# ENDING
159
159
# ------------------------------------------------------------------- #
160
- ENDING_TEMPLATE = r"""<div id='ending'> </div>"""
160
+ ENDING_TEMPLATE = open ('templates/footer.html' , 'r' ).read ()\
161
+ .encode ('utf-8' )
161
162
162
163
163
164
# -------------------- The end of the Template class -------------------
@@ -291,18 +292,22 @@ def sort_result(result_list):
291
292
292
293
class HTMLTestRunner (TemplateMixin ):
293
294
def __init__ (self , stream = sys .stdout , verbosity = 1 , title = None ,
294
- description = None ):
295
+ description = None , attrs = None ):
295
296
self .stream = stream
296
297
self .verbosity = verbosity
298
+
297
299
if title is None :
298
300
self .title = self .DEFAULT_TITLE
299
301
else :
300
302
self .title = title
303
+
301
304
if description is None :
302
305
self .description = self .DEFAULT_DESCRIPTION
303
306
else :
304
307
self .description = description
305
308
309
+ self .attributes = attrs
310
+
306
311
self .startTime = datetime .datetime .now ()
307
312
self .stopTime = None
308
313
@@ -318,61 +323,77 @@ def run(self, test):
318
323
319
324
def get_report_attributes (self , result ):
320
325
"""
321
- Return report attributes as a list of (name, value).
326
+ Return report attributes as a list of tuples with (name, value).
322
327
Override this to add custom attributes.
323
328
"""
324
- start_time = str (self .startTime )[: 19 ]
329
+ start_time = str (self .startTime )
325
330
duration = str (self .stopTime - self .startTime )
326
- status = []
331
+ statuses = []
327
332
if result .success_count :
328
- status .append ('Pass %s' % result .success_count )
333
+ statuses .append ('Passed %s' % result .success_count )
329
334
if result .skip_count :
330
- status .append ('Skip %s' % result .skip_count )
335
+ statuses .append ('Skipped %s' % result .skip_count )
331
336
if result .failure_count :
332
- status .append ('Failure %s' % result .failure_count )
337
+ statuses .append ('Failed %s' % result .failure_count )
333
338
if result .error_count :
334
- status .append ('Error %s' % result .error_count )
335
- if status :
336
- status = ', ' .join (status )
339
+ statuses .append ('Errors %s' % result .error_count )
340
+ if statuses :
341
+ statuses = ', ' .join (statuses )
337
342
else :
338
- status = 'none '
339
- return [
343
+ statuses = 'None '
344
+ group1 = [
340
345
('Start Time' , start_time ),
346
+ ('Stop Time' , str (self .stopTime )),
341
347
('Duration' , duration ),
342
- ('Status' , status ),
348
+ ('Status' , statuses ),
343
349
]
344
350
351
+ return group1
352
+
345
353
def generate_report (self , result ):
346
- report_attrs = self .get_report_attributes (result )
347
354
generator = 'HTMLTestRunner %s' % __version__
348
355
stylesheet = self ._generate_stylesheet ()
349
- heading = self ._generate_heading (report_attrs )
356
+ heading = self ._generate_heading (result )
350
357
report = self ._generate_report (result )
351
358
ending = self ._generate_ending ()
352
359
output = self .HTML_TEMPLATE % dict (
353
360
title = saxutils .escape (self .title ),
354
361
generator = generator ,
355
- stylesheet = stylesheet ,
356
- heading = heading ,
357
- report = report ,
358
- ending = ending ,
362
+ version = __version__ ,
363
+ head_inserts = stylesheet ,
364
+ header = heading ,
365
+ result_table = report ,
366
+ footer = ending ,
359
367
)
360
368
self .stream .write (output .encode ('utf8' ))
361
369
362
370
def _generate_stylesheet (self ):
363
371
return self .STYLESHEET_TEMPLATE
364
372
365
- def _generate_heading (self , report_attrs ):
373
+ def _parse_attributes_group (self , group ):
366
374
attrs_list = []
367
- for attr_name , attr_value in report_attrs :
375
+ for attr_name , attr_value in group :
368
376
attr_line = self .HEADING_ATTRIBUTE_TEMPLATE % dict (
369
377
name = saxutils .escape (attr_name ),
370
378
value = saxutils .escape (attr_value ),
371
379
)
372
380
attrs_list .append (attr_line )
381
+ return attrs_list
382
+
383
+ def _generate_heading (self , result ):
384
+ g1 = self .get_report_attributes (result )
385
+ g2 = self .attributes ['group2' ]
386
+ g3 = self .attributes ['group3' ]
387
+
388
+ pg1 = self ._parse_attributes_group (g1 )
389
+ pg2 = self ._parse_attributes_group (g2 )
390
+ pg3 = self ._parse_attributes_group (g3 )
391
+
373
392
heading = self .HEADING_TEMPLATE % dict (
374
393
title = saxutils .escape (self .title ),
375
- parameters = '' .join (attrs_list ),
394
+ parameters_1 = '' .join (pg1 ),
395
+ parameters_2 = '' .join (pg2 ),
396
+ parameters_3 = '' .join (pg3 ),
376
397
description = saxutils .escape (self .description ),
377
398
)
378
399
return heading
@@ -470,13 +491,13 @@ def _generate_report_test(self, rows, class_id, test_id, n, t, output, e):
470
491
471
492
script = self .REPORT_TEST_OUTPUT_TEMPLATE % dict (
472
493
id = test_id ,
473
- output = saxutils .escape (uo + ue ),
494
+ output = saxutils .escape (uo . strip () + ue . strip () ),
474
495
)
475
496
row = tmpl % dict (
476
497
tid = test_id ,
477
498
Class = (n == 0 and 'hiddenRow' or 'none' ),
478
- style = n == 2 and 'errorCase ' or (
479
- n == 1 and 'failCase ' or n == 3 and 'skipCase ' or 'passCase ' ),
499
+ style = n == 2 and 'bg-info ' or (
500
+ n == 1 and 'bg-danger ' or n == 3 and 'bg-warning ' or 'bg-success ' ),
480
501
desc = desc ,
481
502
script = script ,
482
503
status = self .STATUS [n ],
0 commit comments