Skip to content

Added option to append one or more user-provided style sheets to report's styling. #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions pytest_html/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def pytest_addoption(parser):
'that the report may not render or function where CSP '
'restrictions are in place (see '
'https://developer.mozilla.org/docs/Web/Security/CSP)')
group.addoption('--append-css', action='append', dest='appendcss',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name this simply '--css' and leave the default dest of 'css'?

metavar='path', default=None,
help='append given CSS file content to report style file.')


def pytest_configure(config):
Expand Down Expand Up @@ -92,6 +95,9 @@ def __init__(self, logfile, config):
self.self_contained = config.getoption('self_contained_html')
self.config = config

self.css_append = config.getoption('appendcss')
self.css_errors = []

class TestResult:

def __init__(self, outcome, report, logfile, config):
Expand Down Expand Up @@ -129,7 +135,7 @@ def __init__(self, outcome, report, logfile, config):
if len(cells) > 0:
self.row_table = html.tr(cells)
self.row_extra = html.tr(html.td(self.additional_html,
class_='extra', colspan=len(cells)))
class_='extra', colspan=len(cells)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This change makes the line too long for PEP8.


def __lt__(self, other):
order = ('Error', 'Failed', 'Rerun', 'XFailed',
Expand All @@ -139,7 +145,7 @@ def __lt__(self, other):
def create_asset(self, content, extra_index,
test_index, file_extension, mode='w'):
hash_key = ''.join([self.test_id, str(extra_index),
str(test_index)]).encode('utf-8')
str(test_index)]).encode('utf-8')
hash_generator = hashlib.md5()
hash_generator.update(hash_key)
asset_file_name = '{0}.{1}'.format(hash_generator.hexdigest(),
Expand Down Expand Up @@ -321,6 +327,21 @@ def _generate_report(self, session):
ansi_css.extend([str(r) for r in style.get_styles()])
self.style_css += '\n'.join(ansi_css)

# <DF> Add user-provided CSS
if self.css_append:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe if we're using append, then the default should be an empty list. If so, we shouldn't need this check and could just do for path in self.css:

self.style_css += '\n'
user_css = []
for filename in self.css_append:
try:
with open(filename, 'r') as css_file:
user_css.append('/* Begin CSS from {} */'.format(filename))
user_css.extend(css_file.readlines())
user_css.append('/* End CSS from {} */'.format(filename))
except Exception as e:
self.css_errors.append('Warning: Could not read CSS from {}: {}'.format(filename, e))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than gathering bare exceptions for later reporting, I think we should check that the paths are valid before w run any tests. This can be done in pytest_addoption or pytest_configure. Otherwise, a typo in any CSS path would not be known until the test session is complete, which could be frustrating if the tests take a long time to run.


self.style_css += '\n'.join(user_css)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes sense to keep the CSS files separate instead of appending to a single file. We don't have a choice for the self-contained report, but it might help to keep the assets based report more manageble. We can always revisit this later though.


css_href = '{0}/{1}'.format('assets', 'style.css')
html_css = html.link(href=css_href, rel='stylesheet',
type='text/css')
Expand Down Expand Up @@ -402,10 +423,10 @@ def generate_summary_item(self):
html.tr(cells),
html.tr([
html.th('No results found. Try to check the filters',
colspan=len(cells))],
colspan=len(cells))],
id='not-found-message', hidden='true'),
id='results-table-head'),
self.test_logs], id='results-table')]
self.test_logs], id='results-table')]

main_js = pkg_resources.resource_string(
__name__, os.path.join('resources', 'main.js'))
Expand Down Expand Up @@ -490,3 +511,5 @@ def pytest_sessionfinish(self, session):
def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep('-', 'generated html file: {0}'.format(
self.logfile))
for css_error in self.css_errors:
terminalreporter.write_line(css_error)