Skip to content

Commit f8fcd88

Browse files
i-am-david-fernandezdavehunt
authored andcommitted
Added option to append one or more user-provided style sheets to report's styling.
1 parent ebebda4 commit f8fcd88

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

pytest_html/plugin.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def pytest_addoption(parser):
5555
'that the report may not render or function where CSP '
5656
'restrictions are in place (see '
5757
'https://developer.mozilla.org/docs/Web/Security/CSP)')
58+
group.addoption('--append-css', action='append', dest='appendcss',
59+
metavar='path', default=None,
60+
help='append given CSS file content to report style file.')
5861

5962

6063
def pytest_configure(config):
@@ -92,6 +95,9 @@ def __init__(self, logfile, config):
9295
self.self_contained = config.getoption('self_contained_html')
9396
self.config = config
9497

98+
self.css_append = config.getoption('appendcss')
99+
self.css_errors = []
100+
95101
class TestResult:
96102

97103
def __init__(self, outcome, report, logfile, config):
@@ -129,7 +135,7 @@ def __init__(self, outcome, report, logfile, config):
129135
if len(cells) > 0:
130136
self.row_table = html.tr(cells)
131137
self.row_extra = html.tr(html.td(self.additional_html,
132-
class_='extra', colspan=len(cells)))
138+
class_='extra', colspan=len(cells)))
133139

134140
def __lt__(self, other):
135141
order = ('Error', 'Failed', 'Rerun', 'XFailed',
@@ -139,7 +145,7 @@ def __lt__(self, other):
139145
def create_asset(self, content, extra_index,
140146
test_index, file_extension, mode='w'):
141147
hash_key = ''.join([self.test_id, str(extra_index),
142-
str(test_index)]).encode('utf-8')
148+
str(test_index)]).encode('utf-8')
143149
hash_generator = hashlib.md5()
144150
hash_generator.update(hash_key)
145151
asset_file_name = '{0}.{1}'.format(hash_generator.hexdigest(),
@@ -321,6 +327,21 @@ def _generate_report(self, session):
321327
ansi_css.extend([str(r) for r in style.get_styles()])
322328
self.style_css += '\n'.join(ansi_css)
323329

330+
# <DF> Add user-provided CSS
331+
if self.css_append:
332+
self.style_css += '\n'
333+
user_css = []
334+
for filename in self.css_append:
335+
try:
336+
with open(filename, 'r') as css_file:
337+
user_css.append('/* Begin CSS from {} */'.format(filename))
338+
user_css.extend(css_file.readlines())
339+
user_css.append('/* End CSS from {} */'.format(filename))
340+
except Exception as e:
341+
self.css_errors.append('Warning: Could not read CSS from {}: {}'.format(filename, e))
342+
343+
self.style_css += '\n'.join(user_css)
344+
324345
css_href = '{0}/{1}'.format('assets', 'style.css')
325346
html_css = html.link(href=css_href, rel='stylesheet',
326347
type='text/css')
@@ -402,10 +423,10 @@ def generate_summary_item(self):
402423
html.tr(cells),
403424
html.tr([
404425
html.th('No results found. Try to check the filters',
405-
colspan=len(cells))],
426+
colspan=len(cells))],
406427
id='not-found-message', hidden='true'),
407428
id='results-table-head'),
408-
self.test_logs], id='results-table')]
429+
self.test_logs], id='results-table')]
409430

410431
main_js = pkg_resources.resource_string(
411432
__name__, os.path.join('resources', 'main.js'))
@@ -501,3 +522,5 @@ def pytest_sessionfinish(self, session):
501522
def pytest_terminal_summary(self, terminalreporter):
502523
terminalreporter.write_sep('-', 'generated html file: {0}'.format(
503524
self.logfile))
525+
for css_error in self.css_errors:
526+
terminalreporter.write_line(css_error)

0 commit comments

Comments
 (0)