From 9241d963788b303087105962ccdba3cb451f6e65 Mon Sep 17 00:00:00 2001 From: nirajnagle Date: Tue, 16 Feb 2021 00:03:38 -0500 Subject: [PATCH 1/2] Add logo to htmlreport. --- docs/changelog.rst | 17 +++++++++++++++++ src/pytest_html/hooks.py | 4 ++++ src/pytest_html/html_report.py | 8 +++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index e1f52a22..bf9f700d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,6 +3,23 @@ Changelog Versions follow `Semantic Versioning`_ (``..``). +Version History +--------------- +3.2.1(2021-02-15) + +* Implement functionalitly to add/customize logo on Tests reports page. + + * Report Logo + + * By default the report logo will be None. + + * you can edit it by using the pytest_html_report_logo hook + + Usage: + + def pytest_html_report_logo(report): + report.img_path = 'Path to Logo Image.' + Version History --------------- diff --git a/src/pytest_html/hooks.py b/src/pytest_html/hooks.py index 7b75120b..42f3ad18 100644 --- a/src/pytest_html/hooks.py +++ b/src/pytest_html/hooks.py @@ -21,3 +21,7 @@ def pytest_html_results_table_row(report, cells): def pytest_html_results_table_html(report, data): """ Called after building results table additional HTML. """ + + +def pytest_html_report_logo(report): + """ Called before adding the image to the report. """ diff --git a/src/pytest_html/html_report.py b/src/pytest_html/html_report.py index 66e10f07..48bb554b 100644 --- a/src/pytest_html/html_report.py +++ b/src/pytest_html/html_report.py @@ -23,6 +23,7 @@ def __init__(self, logfile, config): self.logfile = os.path.abspath(logfile) self.test_logs = [] self.title = os.path.basename(self.logfile) + self.img_path = None self.results = [] self.errors = self.failed = 0 self.passed = self.skipped = 0 @@ -181,10 +182,15 @@ def _generate_report(self, session): os.path.join(os.path.dirname(__file__), "resources", "main.js") ) as main_js_fp: main_js = main_js_fp.read() - + + def image_visiblity_on_error(): + if self.img_path is None: + return "this.style.display='none'" + body = html.body( html.script(raw(main_js)), html.h1(self.title), + html.img(src=self.img_path, onerror=image_visiblity_on_error()), html.p( "Report generated on {} at {} by ".format( generated.strftime("%d-%b-%Y"), generated.strftime("%H:%M:%S") From ac8e992cfa54c6c8f04d8f6b626f4e8c56346811 Mon Sep 17 00:00:00 2001 From: nirajnagle Date: Tue, 16 Feb 2021 00:34:44 -0500 Subject: [PATCH 2/2] Resolve linting issues. --- src/pytest_html/hooks.py | 2 +- src/pytest_html/html_report.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pytest_html/hooks.py b/src/pytest_html/hooks.py index 42f3ad18..1937f760 100644 --- a/src/pytest_html/hooks.py +++ b/src/pytest_html/hooks.py @@ -24,4 +24,4 @@ def pytest_html_results_table_html(report, data): def pytest_html_report_logo(report): - """ Called before adding the image to the report. """ + """ Called before adding the image to the report. """ diff --git a/src/pytest_html/html_report.py b/src/pytest_html/html_report.py index 48bb554b..4528a730 100644 --- a/src/pytest_html/html_report.py +++ b/src/pytest_html/html_report.py @@ -182,11 +182,11 @@ def _generate_report(self, session): os.path.join(os.path.dirname(__file__), "resources", "main.js") ) as main_js_fp: main_js = main_js_fp.read() - + def image_visiblity_on_error(): if self.img_path is None: return "this.style.display='none'" - + body = html.body( html.script(raw(main_js)), html.h1(self.title),