Package: pytest-html-lee
pytest-html is a plugin forpytestthat generates a
HTML report for the test results.RequirementsYou will need the following prerequisites in order to use pytest-html:Python 3.6+ or PyPy3InstallationTo install pytest-html:$pipinstallpytest-htmlThen run your tests with:$pytest--html=report.htmlANSI codesNote that ANSI code support depends on theansi2htmlpackage. Due to the use
of a less permissive license, this package is not included as a dependency. If
you have this package installed, then ANSI codes will be converted to HTML in
your report.Creating a self-contained reportIn order to respect theContent Security Policy (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:$pytest--html=report.html--self-contained-htmlImages added as files or links are going to be linked as external resources,
meaning that the standalone report HTML-file may not display these images
as expected.The plugin will issue a warning when adding files or links to the standalone report.Enhancing reportsAppearanceCustom CSS (Cascasding Style Sheets) can be passed on the command line using
the--cssoption. These will be applied in the order specified, and can
be used to change the appearance of the report.$pytest--html=report.html--css=highcontrast.css--css=accessible.cssReport TitleBy default report title will be the filename of the report, you can edit it by using the :code:pytest_html_report_titlehook:importpytestfrompy.xmlimporthtmldefpytest_html_report_title(report)report.title="My very own title!"EnvironmentTheEnvironmentsection is provided by thepytest-metadata, plugin, and can be accessed
via thepytest_configurehook:defpytest_configure(config):config._metadata['foo']='bar'The generated table will be sorted alphabetically unless the metadata is acollections.OrderedDict.Additional summary informationYou can edit theSummarysection by using thepytest_html_results_summaryhook:importpytestfrompy.xmlimporthtmldefpytest_html_results_summary(prefix,summary,postfix):prefix.extend([html.p("foo: bar")])Extra contentYou can add details to the HTML reports by creating an ‘extra’ list on the
report object. Here are the types of extra content that can be added:TypeExampleRaw HTMLextra.html('<div>AdditionalHTML</div>')JSONextra.json({'name':'pytest'})Plain textextra.text('Addsome simple Text')URLextra.url('http://www.example.com/')Imageextra.image(image,mime_type='image/gif',extension='gif')Imageextra.image('/path/to/file.png')Imageextra.image('http://some_image.png')Note: When adding an image from file, the path can be either absolute
or relative.Note: When using--self-contained-html, images added as files or links
may not work as expected, see sectionCreating a self-contained reportfor
more info.There are also convenient types for several image formats:Image formatExamplePNGextra.png(image)JPEGextra.jpg(image)SVGextra.svg(image)The following example adds the various types of extras using apytest_runtest_makereporthook, which can be implemented in a plugin or
conftest.py file:importpytest@pytest.hookimpl(hookwrapper=True)defpytest_runtest_makereport(item,call):pytest_html=item.config.pluginmanager.getplugin('html')outcome=yieldreport=outcome.get_result()extra=getattr(report,'extra',[])ifreport.when=='call':# always add url to reportextra.append(pytest_html.extras.url('http://www.example.com/'))xfail=hasattr(report,'wasxfail')if(report.skippedandxfail)or(report.failedandnotxfail):# only add additional html on failureextra.append(pytest_html.extras.html('<div>Additional HTML</div>'))report.extra=extraYou can also specify thenameargument for all types other thanhtmlwhich will change the title of the
created hyper link:extra.append(pytest_html.extras.text('some string',name='Different title'))It is also possible to use the fixtureextrato add content directly
in a test function without implementing hooks. These will generally end up
before any extras added by plugins.frompytest_htmlimportextrasdeftest_extra(extra):extra.append(extras.text('some string'))Modifying the results tableYou can modify the columns by implementing custom hooks for the header and
rows. The following exampleconftest.pyadds a description column with
the test function docstring, adds a sortable time column, and removes the links
column:fromdatetimeimportdatetimefrompy.xmlimporthtmlimportpytestdefpytest_html_results_table_header(cells):cells.insert(2,html.th('Description'))cells.insert(1,html.th('Time',class_='sortable time',col='time'))cells.pop()defpytest_html_results_table_row(report,cells):cells.insert(2,html.td(report.description))cells.insert(1,html.td(datetime.utcnow(),class_='col-time'))cells.pop()@pytest.hookimpl(hookwrapper=True)defpytest_runtest_makereport(item,call):outcome=yieldreport=outcome.get_result()report.description=str(item.function.__doc__)You can also remove results by implementing thepytest_html_results_table_rowhook and removing all cells. The
following example removes all passed results from the report:importpytestdefpytest_html_results_table_row(report,cells):ifreport.passed:delcells[:]The log output and additional HTML can be modified by implementing thepytest_html_results_htmlhook. The following example replaces all
additional HTML and log output with a notice that the log is empty:importpytestdefpytest_html_results_table_html(report,data):ifreport.passed:deldata[:]data.append(html.div('No log output captured.',class_='empty log'))Display optionsBy default, all rows in theResultstable will be expanded except those that havePassed.This behavior can be customized either with a query parameter:?collapsed=Passed,XFailed,Skippedor by setting therender_collapsedin a configuration file (pytest.ini, setup.cfg, etc).[pytest]render_collapsed=TrueNOTE:Settingrender_collapsedwill, unlike the query parameter, affect all statuses.ScreenshotsContributingWe welcome contributions.To learn more, seeDevelopmentResourcesRelease NotesIssue TrackerCode