Create a separate CSS file by default (#63)
CSS can still be generated inline with --self-contained-html command line option.
This commit is contained in:
parent
a1956abb4d
commit
32fc236b5b
13
README.rst
13
README.rst
|
@ -52,6 +52,19 @@ Then run your tests with:
|
|||
|
||||
$ py.test --html=report.html
|
||||
|
||||
Creating a self-contained report
|
||||
----------------------------------
|
||||
|
||||
In order to respect the `Content Security Policy (CSP)
|
||||
<https://developer.mozilla.org/docs/Web/Security/CSP>`_,
|
||||
several assets such as CSS and images are stored separately by default.
|
||||
You can alternatively create a self-contained report, which can be more
|
||||
convenient when sharing your results. This can be done in the following way:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ py.test --html=report.html --self-contained-html
|
||||
|
||||
Enhancing reports
|
||||
-----------------
|
||||
|
||||
|
|
|
@ -42,6 +42,12 @@ def pytest_addoption(parser):
|
|||
group.addoption('--html', action='store', dest='htmlpath',
|
||||
metavar='path', default=None,
|
||||
help='create html report file at given path.')
|
||||
group.addoption('--self-contained-html', action='store_true',
|
||||
help='create a self-contained html file containing all '
|
||||
'necessary styles, scripts, and images - this means '
|
||||
'that the report may not render or function where CSP '
|
||||
'restrictions are in place (see '
|
||||
'https://developer.mozilla.org/docs/Web/Security/CSP)')
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
|
@ -224,15 +230,20 @@ class HTMLReport(object):
|
|||
numtests = self.passed + self.failed + self.xpassed + self.xfailed
|
||||
generated = datetime.datetime.now()
|
||||
|
||||
style_css = pkg_resources.resource_string(
|
||||
self.style_css = pkg_resources.resource_string(
|
||||
__name__, os.path.join('resources', 'style.css'))
|
||||
if PY3:
|
||||
style_css = style_css.decode('utf-8')
|
||||
self.style_css = self.style_css.decode('utf-8')
|
||||
|
||||
html_css = html.link(href='style.css', rel='stylesheet',
|
||||
type='text/css')
|
||||
if session.config.getoption('self_contained_html'):
|
||||
html_css = html.style(raw(self.style_css))
|
||||
|
||||
head = html.head(
|
||||
html.meta(charset='utf-8'),
|
||||
html.title('Test Report'),
|
||||
html.style(raw(style_css)))
|
||||
html_css)
|
||||
|
||||
class Outcome:
|
||||
|
||||
|
@ -333,12 +344,16 @@ class HTMLReport(object):
|
|||
unicode_doc = unicode_doc.decode('utf-8')
|
||||
return unicode_doc
|
||||
|
||||
def _save_report(self, report_content):
|
||||
if not os.path.exists(os.path.dirname(self.logfile)):
|
||||
os.makedirs(os.path.dirname(self.logfile))
|
||||
logfile = open(self.logfile, 'w', encoding='utf-8')
|
||||
logfile.write(report_content)
|
||||
logfile.close()
|
||||
def _save_report(self, report_content, self_contained_html):
|
||||
dir_name = os.path.dirname(self.logfile)
|
||||
if not os.path.exists(dir_name):
|
||||
os.makedirs(dir_name)
|
||||
with open(self.logfile, 'w', encoding='utf-8') as f:
|
||||
f.write(report_content)
|
||||
if not self_contained_html:
|
||||
style_path = os.path.join(dir_name, 'style.css')
|
||||
with open(style_path, 'w', encoding='utf-8') as f:
|
||||
f.write(self.style_css)
|
||||
|
||||
def pytest_runtest_logreport(self, report):
|
||||
if report.passed:
|
||||
|
@ -355,7 +370,8 @@ class HTMLReport(object):
|
|||
|
||||
def pytest_sessionfinish(self, session):
|
||||
report_content = self._generate_report(session)
|
||||
self._save_report(report_content)
|
||||
self_contained_html = session.config.getoption('self_contained_html')
|
||||
self._save_report(report_content, self_contained_html)
|
||||
|
||||
def pytest_terminal_summary(self, terminalreporter):
|
||||
terminalreporter.write_sep('-', 'generated html file: {0}'.format(
|
||||
|
|
|
@ -174,17 +174,32 @@ class TestHTML:
|
|||
assert result.ret == 0
|
||||
assert_results(html)
|
||||
|
||||
def test_resources_inline_css(self, testdir):
|
||||
testdir.makepyfile('def test_pass(): pass')
|
||||
result, html = run(testdir, 'report.html', '--self-contained-html')
|
||||
assert result.ret == 0
|
||||
|
||||
content = pkg_resources.resource_string(
|
||||
'pytest_html', os.path.join('resources', 'style.css'))
|
||||
if PY3:
|
||||
content = content.decode('utf-8')
|
||||
assert content
|
||||
assert content in html
|
||||
|
||||
def test_resources(self, testdir):
|
||||
testdir.makepyfile('def test_pass(): pass')
|
||||
result, html = run(testdir)
|
||||
assert result.ret == 0
|
||||
for resource in ['style.css', 'main.js']:
|
||||
content = pkg_resources.resource_string(
|
||||
'pytest_html', os.path.join('resources', resource))
|
||||
if PY3:
|
||||
content = content.decode('utf-8')
|
||||
assert content
|
||||
assert content in html
|
||||
|
||||
content = pkg_resources.resource_string(
|
||||
'pytest_html', os.path.join('resources', 'main.js'))
|
||||
if PY3:
|
||||
content = content.decode('utf-8')
|
||||
assert content
|
||||
assert content in html
|
||||
|
||||
regex_css_link = '<link href="style.css" rel="stylesheet"'
|
||||
assert re.search(regex_css_link, html) is not None
|
||||
|
||||
def test_stdout(self, testdir):
|
||||
content = (str(random.random()), str(random.random()))
|
||||
|
|
Loading…
Reference in New Issue